Skip to content

Commit

Permalink
Merge pull request #110 from devnexen/fbsd_part3
Browse files Browse the repository at this point in the history
freebsd support mostly adding its own wrapper.
  • Loading branch information
emeryberger authored May 4, 2024
2 parents 2247f5b + f857fbd commit 111d216
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
68 changes: 68 additions & 0 deletions src/fbsd_wrapper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
// Copyright 2024 The Heap-Layers and Mesh Authors. All rights reserved.
// Use of this source code is governed by the Apache License,
// Version 2.0, that can be found in the LICENSE file.

#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <new>

/*
To use this library,
you only need to define the following allocation functions:
- xxmalloc
- xxfree
- xxmalloc_usable_size
- xxmalloc_lock
- xxmalloc_unlock
See the extern "C" block below for function prototypes and more
details. YOU SHOULD NOT NEED TO MODIFY ANY OF THE CODE HERE TO
SUPPORT ANY ALLOCATOR.
LIMITATIONS:
- This wrapper assumes that the underlying allocator will do "the
right thing" when xxfree() is called with a pointer internal to an
allocated object. Header-based allocators, for example, need not
apply.
*/

#define WEAK(x) __attribute__((weak, alias(#x)))
#ifndef __THROW
#define __THROW
#endif

#ifndef CACHELINE_ALIGNED_FN
#define ATTRIBUTE_ALIGNED(s) __attribute__((aligned(s)))
#define CACHELINE_SIZE 64
#define CACHELINE_ALIGNED_FN ATTRIBUTE_ALIGNED(CACHELINE_SIZE)
#endif

#define CUSTOM_PREFIX(x) mesh_##x

#define WEAK_REDEF1(type, fname, arg1) MESH_EXPORT type fname(arg1) __THROW WEAK(mesh_##fname)
#define WEAK_REDEF2(type, fname, arg1, arg2) MESH_EXPORT type fname(arg1, arg2) __THROW WEAK(mesh_##fname)
#define WEAK_REDEF3(type, fname, arg1, arg2, arg3) MESH_EXPORT type fname(arg1, arg2, arg3) __THROW WEAK(mesh_##fname)

extern "C" {
WEAK_REDEF1(void *, malloc, size_t);
WEAK_REDEF1(void, free, void *);
WEAK_REDEF1(void, cfree, void *);
WEAK_REDEF2(void *, calloc, size_t, size_t);
WEAK_REDEF2(void *, realloc, void *, size_t);
WEAK_REDEF2(void *, reallocarray, void *, size_t, size_t);
WEAK_REDEF2(void *, memalign, size_t, size_t);
WEAK_REDEF3(int, posix_memalign, void **, size_t, size_t);
WEAK_REDEF2(void *, aligned_alloc, size_t, size_t);
WEAK_REDEF1(size_t, malloc_usable_size, const void *);
}

#include "wrapper.cc"
20 changes: 19 additions & 1 deletion src/libmesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,23 @@ extern "C" MESH_EXPORT CACHELINE_ALIGNED_FN void *mesh_realloc(void *oldPtr, siz
return localHeap->realloc(oldPtr, newSize);
}

#if defined(__FreeBSD__)
extern "C" MESH_EXPORT CACHELINE_ALIGNED_FN void *mesh_reallocarray(void *oldPtr, size_t count, size_t size) {
size_t total;
if (unlikely(__builtin_umull_overflow(count, size, &total))) {
return NULL;
} else {
return mesh_realloc(oldPtr, total);
}
}
#endif

#ifndef __FreeBSD__
extern "C" MESH_EXPORT CACHELINE_ALIGNED_FN size_t mesh_malloc_usable_size(void *ptr) {
#else
extern "C" MESH_EXPORT CACHELINE_ALIGNED_FN size_t mesh_malloc_usable_size(const void *cptr) {
void *ptr = const_cast<void *>(cptr);
#endif
ThreadLocalHeap *localHeap = ThreadLocalHeap::GetHeapIfPresent();
if (unlikely(localHeap == nullptr)) {
return mesh::usableSizeSlowpath(ptr);
Expand Down Expand Up @@ -244,6 +260,8 @@ ssize_t MESH_EXPORT recvmsg(int sockfd, struct msghdr *msg, int flags) {
#include "gnu_wrapper.cc"
#elif defined(__APPLE__)
#include "mac_wrapper.cc"
#elif defined(__FreeBSD__)
#include "fbsd_wrapper.cc"
#else
#error "only linux and macOS support for now"
#error "only linux, macOS and FreeBSD support for now"
#endif
4 changes: 4 additions & 0 deletions src/wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ void *xxmalloc(size_t);
void xxfree(void *);

// Takes a pointer and returns how much space it holds.
#ifndef __FreeBSD__
size_t xxmalloc_usable_size(void *);
#else
size_t xxmalloc_usable_size(const void *);
#endif

// Locks the heap(s), used prior to any invocation of fork().
void xxmalloc_lock();
Expand Down

0 comments on commit 111d216

Please sign in to comment.