Compare commits
25 Commits
85713d25c1
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 66dfd956da | |||
| c7d175d90f | |||
| fb0efd9cbb | |||
| 735c5cb77e | |||
| d58e24f6a9 | |||
| d1a45cf8e0 | |||
| 36457f775f | |||
| afc3148de3 | |||
| a695653e27 | |||
| a4cae42114 | |||
| 24d9994c87 | |||
| d85735add9 | |||
| 0c6301954f | |||
| fc418f6163 | |||
| aba9cba602 | |||
| 64a5b20941 | |||
| 0073924050 | |||
| 0ca7f64c83 | |||
| 1b61b3c6d0 | |||
| 055338b1e9 | |||
| 271c2a611f | |||
| 88e9f95988 | |||
| a4547f6736 | |||
| 4e28b2255c | |||
| b2f834b7cd |
Vendored
+1
-1
@@ -7,5 +7,5 @@
|
||||
"$(workspaceFolder)/include/",
|
||||
|
||||
],
|
||||
"C_Cpp.default.cStandard": "gnu99"
|
||||
"C_Cpp.default.cStandard": "gnu17"
|
||||
}
|
||||
|
||||
+4
-3
@@ -10,7 +10,7 @@ set(libucore_VERSION ${libucore_VERSION_MAJOR}.${libucore_VERSION_MINOR}.${libuc
|
||||
|
||||
set(CMAKE_VERBOSE_MAKEFILE ON)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD 17)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wundef -Wcast-qual -Wshadow -Wcast-align -pipe -Wno-unused-parameter -Werror=implicit-function-declaration -Winit-self")
|
||||
add_definitions(-D_FILE_OFFSET_BITS=64)
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -O2")
|
||||
@@ -30,13 +30,14 @@ if (CODE_COVERAGE)
|
||||
set(build_type "${build_type}-coverage")
|
||||
endif()
|
||||
|
||||
add_compile_definitions($<$<CONFIG:Debug>:DEBUG>)
|
||||
option(BUILD_SANITIZE "build with address sanitizer" OFF)
|
||||
if (BUILD_SANITIZE)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
|
||||
set(build_type "${build_type}-sanitize")
|
||||
endif()
|
||||
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(CHECK REQUIRED IMPORTED_TARGET check)
|
||||
include_directories(include)
|
||||
|
||||
add_subdirectory(src)
|
||||
|
||||
-177
@@ -1,177 +0,0 @@
|
||||
import os
|
||||
import platform
|
||||
|
||||
#version info of the library, we use 5 chars from the git hash
|
||||
revision = os.popen('git describe --abbrev=5 --dirty --always').read()[:-1]
|
||||
|
||||
version_info = {
|
||||
'version_major': 1 ,
|
||||
'version_minor': 0 ,
|
||||
'version_patch': 0 ,
|
||||
'version_revision': revision,
|
||||
#version_abi is the abi version of the shared library. Only bump it when
|
||||
#backwards compatibility breaks. Strive to not break the ABI.
|
||||
'version_abi': '1.0.0'
|
||||
}
|
||||
version_info['version_str'] = "%d.%d.%d.%s" % (
|
||||
version_info['version_major'],
|
||||
version_info['version_minor'],
|
||||
version_info['version_patch'],
|
||||
version_info['version_revision'])
|
||||
|
||||
|
||||
AddOption('--build_type',
|
||||
dest = 'build_type',
|
||||
type = 'choice',
|
||||
nargs = 1,
|
||||
action='store',
|
||||
default='debug',
|
||||
metavar='TYPE',
|
||||
choices = ['debug', 'release'],
|
||||
help = 'debug|release - Build a Debug (default) or Release variant'
|
||||
)
|
||||
AddOption('--prefix',
|
||||
dest='prefix',
|
||||
type='string',
|
||||
nargs=1,
|
||||
action='store',
|
||||
metavar='DIR',
|
||||
default='/usr',
|
||||
help='installation prefix'
|
||||
)
|
||||
|
||||
AddOption('--test_xml',
|
||||
dest='test_xml',
|
||||
action='store_true',
|
||||
help='Create a result.xml when running the testsuite'
|
||||
)
|
||||
|
||||
AddOption('--coverage',
|
||||
dest='coverage',
|
||||
action='store_true',
|
||||
help='Build with coverage (gcov) support'
|
||||
)
|
||||
|
||||
AddOption('--without-assertions',
|
||||
dest='assertions',
|
||||
action='store_false',
|
||||
default=True,
|
||||
help='Build without assertions enabled'
|
||||
)
|
||||
|
||||
AddOption('--stack-protection',
|
||||
dest = 'stack_protection',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help = 'Build with stack protection support'
|
||||
)
|
||||
|
||||
AddOption('--sanitizer',
|
||||
dest = 'sanitizer',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help = 'Build with gcc sanitizer'
|
||||
)
|
||||
|
||||
AddOption('--32',
|
||||
dest='force32bit',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Force building for 32 bit architecture'
|
||||
)
|
||||
|
||||
build_type = GetOption('build_type')
|
||||
assertions = GetOption('assertions')
|
||||
stack_protection = GetOption('stack_protection')
|
||||
test_xml = GetOption('test_xml')
|
||||
prefix = GetOption('prefix')
|
||||
lib_suffix = ''
|
||||
if not (build_type in ['debug', 'release']):
|
||||
print("Error: expected 'debug' or 'release', found: " + build_type)
|
||||
Exit(1)
|
||||
|
||||
def InstallPerm(env, dest, files, perm):
|
||||
obj = env.Install(dest, files)
|
||||
for i in obj:
|
||||
env.AddPostAction(i, Chmod(str(i), perm))
|
||||
return obj
|
||||
|
||||
|
||||
env = Environment(tools = ['default', 'textfile', 'packaging'])
|
||||
|
||||
#allow shell environment to override compiler
|
||||
if os.environ.get('CC'):
|
||||
env['CC'] = os.environ['CC']
|
||||
|
||||
env.AddMethod(InstallPerm)
|
||||
|
||||
env.Append(CFLAGS = ['-std=gnu99','-Wall', '-Wextra', '-Wundef', '-Wcast-qual','-Wshadow' ,'-Wcast-align','-pipe', '-ggdb', '-pthread'])
|
||||
env.Append(CFLAGS = ['-Wno-unused-parameter', '-Werror=implicit-function-declaration', '-Winit-self'])
|
||||
env.Append(LINKFLAGS = ['-O2', '-pthread'])
|
||||
env.Append(CPPDEFINES = ['_FILE_OFFSET_BITS=64'])
|
||||
env.Append(CPPPATH = ['#include'])
|
||||
env.Append(SHLIBVERSION = version_info['version_abi'])
|
||||
|
||||
if GetOption('force32bit'):
|
||||
env.Append(CFLAGS = ['-m32'])
|
||||
env.Append(LINKFLAGS = ['-m32'])
|
||||
|
||||
if build_type == 'release':
|
||||
env.Append(CFLAGS = ['-O2'])
|
||||
elif build_type == 'debug':
|
||||
lib_suffix = 'D'
|
||||
env.Append(CPPDEFINES = ['DEBUG'])
|
||||
|
||||
if stack_protection:
|
||||
env.Append(CFLAGS = ['-fstack-protector', '--param=ssp-buffer-size=4'])
|
||||
env.Append(CPPDEFINES = ['_FORTIFY_SOURCE=2'])
|
||||
if not assertions:
|
||||
#this will turn off assert()
|
||||
env.Append(CPPDEFINES = ['NDEBUG'])
|
||||
|
||||
if GetOption('coverage'):
|
||||
env.Append(CFLAGS = ['--coverage'])
|
||||
env.Append(LINKFLAGS = ['--coverage'])
|
||||
|
||||
if GetOption('sanitizer'):
|
||||
env.Append(CFLAGS = ['-fsanitize=undefined', '-fsanitize=address'])
|
||||
env.Append(LINKFLAGS = ['-fsanitize=undefined', '-fsanitize=address'])
|
||||
|
||||
# Generate substitute dictionary
|
||||
subst_info = dict(('@' + key + '@', version_info[key]) for key in version_info)
|
||||
subst_info['@prefix@'] = prefix
|
||||
subst_info['@build_type@'] = build_type
|
||||
|
||||
if assertions:
|
||||
subst_info['@build_type@'] += '_A'
|
||||
if stack_protection:
|
||||
subst_info['@build_type@'] += '_SP'
|
||||
|
||||
subst_info['@build_host@'] = platform.node()
|
||||
|
||||
Export('env', 'build_type', 'prefix', 'lib_suffix', 'version_info', 'test_xml', 'subst_info')
|
||||
|
||||
#put all .sconsign files in one place
|
||||
env.SConsignFile()
|
||||
|
||||
subdirs = [
|
||||
'src',
|
||||
'include',
|
||||
'test',
|
||||
]
|
||||
|
||||
for subdir in subdirs:
|
||||
env.SConscript(subdir + '/SConscript', variant_dir='#build/' + subdir, src_dir='#'+subdir, duplicate=0)
|
||||
|
||||
env.Package(target = 'libucore-' + version_info['version_str'] + '.tar.gz',
|
||||
source = env.FindInstalledFiles(),
|
||||
NAME = 'libucore',
|
||||
VERSION = version_info['version_str'],
|
||||
PACKAGEVERSION = 0,
|
||||
PACKAGETYPE = 'targz',
|
||||
LICENSE = 'BSD',
|
||||
SUMMARY = 'libucore is a C library of misc useful stuff including an eventloop, timer support and various data-structures.',
|
||||
DESCRIPTION = 'libucore is a C library of misc useful stuff including an eventloop, timer support and various data-structures.',
|
||||
X_RPM_GROUP = 'Development/Libraries'
|
||||
)
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import os
|
||||
|
||||
Import('env', 'build_type', 'prefix', 'version_info')
|
||||
|
||||
ucore_env = env.Clone()
|
||||
|
||||
headers = ucore_env.Glob('ucore/*.h')
|
||||
|
||||
#install targets
|
||||
|
||||
ucore_env.Alias('install',
|
||||
ucore_env.InstallPerm(os.path.join(prefix, 'include', 'ucore'), headers, 0o0644))
|
||||
|
||||
@@ -19,9 +19,40 @@ uc_phi_32(uint32_t N);
|
||||
uint64_t
|
||||
uc_phi_64(uint64_t N);
|
||||
|
||||
#if defined(__has_builtin)
|
||||
# if __has_builtin(__builtin_clzll)
|
||||
# define HAVE_BUILTIN_CLZ 1
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
// Note: the nextpow2 functions have undefined behavior if
|
||||
// x > 2^(n-1) for n of the 32 or 64 bit variants
|
||||
|
||||
#if HAVE_BUILTIN_CLZ
|
||||
|
||||
static inline uint32_t
|
||||
uc_nextpow2(uint32_t x)
|
||||
{
|
||||
if (x <= 1) return 1;
|
||||
return 1u << (32 - __builtin_clz(x - 1));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline uint64_t
|
||||
uc_nextpow2_64(uint64_t x)
|
||||
{
|
||||
if (x <= 1) return 1;
|
||||
return (uint64_t)1 << (64 - __builtin_clzll(x - 1));
|
||||
return x;
|
||||
}
|
||||
#else
|
||||
static inline uint32_t
|
||||
uc_nextpow2(uint32_t x)
|
||||
{
|
||||
if (x == 0) {
|
||||
return 1;
|
||||
}
|
||||
x--;
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
@@ -33,6 +64,26 @@ uc_nextpow2(uint32_t x)
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline uint64_t
|
||||
uc_nextpow2_64(uint64_t x)
|
||||
{
|
||||
if (x == 0) {
|
||||
return 1;
|
||||
}
|
||||
x--;
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
x |= x >> 4;
|
||||
x |= x >> 8;
|
||||
x |= x >> 16;
|
||||
x |= x >> 32;
|
||||
x++;
|
||||
|
||||
return x;
|
||||
}
|
||||
#endif
|
||||
|
||||
#undef HAVE_BUILTIN_CLZ
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
#ifndef UC_SARENA_H_
|
||||
#define UC_SARENA_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @file
|
||||
* Static arena allocator.
|
||||
*
|
||||
* An UCSArena allocates memory from a provided buffer.
|
||||
*
|
||||
* The backing buffer should be suitable aligned to the types
|
||||
* that will be allocated from it. Use .e.g
|
||||
* @code
|
||||
* _Alignas(16) char mem[1024];
|
||||
* @endcode
|
||||
* as the backing buffer.
|
||||
*
|
||||
* Once allocated, a piece of memory obtained from the
|
||||
* allocator cannot be free'd individually, only the entire SArena can
|
||||
* be free'd/reset
|
||||
*/
|
||||
typedef struct UCSArena UCSArena;
|
||||
struct UCSArena {
|
||||
char *start; // start of user buffer
|
||||
char *curr; // next alloc point
|
||||
char *end; // one past end of user buffer
|
||||
};
|
||||
|
||||
/** Initialize a UCSArena with predefined backing array.
|
||||
*
|
||||
* @data must be an array type
|
||||
* Use as
|
||||
* @code
|
||||
* _Alignas(16) char v[128];
|
||||
* UCSArena a = UC_BV_STATIC_INITIALIZER(v);
|
||||
* @endcode
|
||||
*/
|
||||
#define UC_SAR_STATIC_INITIALIZER(data)\
|
||||
UC_SAR_INITIALIZER((data), sizeof(data))
|
||||
|
||||
/** Initializer for UCSArena with predefined memory
|
||||
* Use as
|
||||
* @code
|
||||
* char v[128];
|
||||
* UCSArena a = UC_BV_STATIC_INITIALIZER(v, sizeof v);
|
||||
* @endcode
|
||||
* or
|
||||
* @code
|
||||
* char *m = malloc(128);
|
||||
* UCSArena a = UC_BV_INIT(m, 128);
|
||||
* @endcode
|
||||
*/
|
||||
#define UC_SAR_INITIALIZER(data, sz)\
|
||||
{\
|
||||
(data),\
|
||||
(data),\
|
||||
(data) + sz\
|
||||
}
|
||||
|
||||
/** Initialize a UCSArena with an array as the backing memory to allocate from.
|
||||
* The macro can be used at local scope to allocate memory automatic on the stack,
|
||||
* or at global scope where the backing array will have static storage duration
|
||||
*
|
||||
* Use as:
|
||||
* @code
|
||||
* UC_SAR_ALLOC_DEF(my_allocator, 1024)
|
||||
* struct foo *foo = uc_sar_alloc(&my_allocator);
|
||||
* if (foo == NULL) {
|
||||
* // out of memory
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @note, the backing buffer has alignment of 16. Use manual init if a different alignment
|
||||
* is needed.
|
||||
* @param var_name Variable name for the allocator
|
||||
* @param size_ byte size of the backing array
|
||||
*/
|
||||
#define UC_SAR_ALLOC_DEF(var_name, size_) \
|
||||
struct {\
|
||||
_Alignas(16) char memory[size_]; \
|
||||
} var_name ## _memory;\
|
||||
UCSArena var_name = UC_SAR_STATIC_INITIALIZER(var_name ## _memory.memory)
|
||||
|
||||
/** Allocate space for @type_ , returned memory is aligned to requirements
|
||||
* of @type.
|
||||
*
|
||||
* Use as:
|
||||
* @code
|
||||
* UC_SAR_ALLOC_DEF(my_allocator, 1024)
|
||||
* struct foo *foo = UC_SAR_ALLOC_ALIGNED(&my_allocator, struct foo);
|
||||
* // Array types can be used too:
|
||||
* struct Foo *str = UC_SAR_ALLOC_ALIGNED(&my_allocator, struct Foo[32]);
|
||||
* @endcode
|
||||
*/
|
||||
#define UC_SAR_ALLOC_ALIGNED(allocator, type_) \
|
||||
uc_sar_alloc_aligned((allocator), sizeof (type_), _Alignof(type_))
|
||||
|
||||
//Initialize an UCSArena to allocate from @mem which is @sz bytes
|
||||
static inline void uc_sar_init(UCSArena *a, void *mem, size_t sz)
|
||||
{
|
||||
a->start = a->curr = mem;
|
||||
a->end = a->start + sz;
|
||||
}
|
||||
|
||||
// Allocate @sz bytes from the UCSArena. Returns NULL if not enough space
|
||||
void *uc_sar_alloc(UCSArena *a, size_t sz);
|
||||
|
||||
// Allocate @sz bytes from the UCSArena, returned pointer is aligned to @align
|
||||
// Returns NULL if not enough space
|
||||
void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align) [[gnu::alloc_align(2)]];
|
||||
|
||||
|
||||
static inline void uc_sar_reset(UCSArena *a)
|
||||
{
|
||||
a->curr = a->start;
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,112 +0,0 @@
|
||||
#ifndef UC_SLOT_ALLOCATOR_H_
|
||||
#define UC_SLOT_ALLOCATOR_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "ucore/bitvec.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/** @file
|
||||
* Fixed size slot memory allocator
|
||||
*
|
||||
* UCSlotAlloc is used to allocate fixed slots of memory from a pre-allocated
|
||||
* piece of memory.
|
||||
*
|
||||
* The main use case is to keep allocated memory in a contiguous block for use
|
||||
* maximum number of objects(slots) is known and relatively small. More than 131072 slots
|
||||
* is not recommended.
|
||||
*
|
||||
* Note that this allocator is not thread safe.
|
||||
*
|
||||
* A bitmap is used to track which slots are allocated.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
typedef struct UCSlotAlloc UCSlotAlloc;
|
||||
struct UCSlotAlloc {
|
||||
const uint32_t slot_size;
|
||||
const uint32_t num_slots;
|
||||
struct UCBitVec bitmap;
|
||||
uint8_t *slots;
|
||||
};
|
||||
|
||||
|
||||
/** Initialize a UCSlotAlloc with an array as the backing memory to allocate slots from.
|
||||
* The macro can be used at local scope to allocate memory automatic (stack) storage,
|
||||
* or at global scope where the backin array will have static storage duration
|
||||
*
|
||||
* Use as:
|
||||
* @code
|
||||
* UC_SLOT_ALLOC_DEF(my_allocator, sizeof foo, 1024)
|
||||
* struct foo *foo = uc_slot_alloc(&my_allocator);
|
||||
* if (foo == NULL) {
|
||||
* // out of memory
|
||||
* } else {
|
||||
* .. use foo
|
||||
* uc_slot_free(&my_allocator, foo);
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @param var_name Variable name for the allocator
|
||||
* @param v slot_size Memory size allocated for each slot
|
||||
* @param v num_slots Number of slots to reserve memory for
|
||||
*/
|
||||
#define UC_SLOT_ALLOC_DEF(var_name, slot_size_, num_slots_) \
|
||||
struct {\
|
||||
uc_bv_integer bitmap[UC_BV_LEN((num_slots_))]; \
|
||||
uint8_t memory[(num_slots_) * (slot_size_)] __attribute__((aligned)); \
|
||||
} var_name ## _memory = { \
|
||||
}; \
|
||||
UCSlotAlloc var_name = { \
|
||||
.slot_size = (slot_size_), \
|
||||
.num_slots = (num_slots_), \
|
||||
.bitmap = UC_BV_STATIC_INIT(var_name ## _memory.bitmap), \
|
||||
.slots = var_name ## _memory.memory \
|
||||
};
|
||||
|
||||
/** Similar to UC_SLOT_ALLOC_DEF but allows you to specify the backing memory that must
|
||||
* be atleast as large as num_slots * slot_size. Note that an array of num_slots * slot_size
|
||||
* bits is also allocated for the bitmap with this definition
|
||||
*/
|
||||
#define UC_SLOT_ALLOC_DEF_EX(var_name, slot_size_, num_slots_, memory) \
|
||||
uc_bv_integer var_name ## _bitmap[UC_BV_LEN((num_slots_))] = {}; \
|
||||
UCSlotAlloc var_name = { \
|
||||
.slot_size = (slot_size_), \
|
||||
.num_slots = (num_slots_), \
|
||||
.bitmap = UC_BV_STATIC_INIT(var_name ## _bitmap), \
|
||||
.slots = (memory) \
|
||||
};
|
||||
|
||||
/** Allocate a memory slot
|
||||
*
|
||||
* @param sa The allocator to allocate from
|
||||
* @return A pointer to the memory slot that's sa.slot_size big, or NULL if
|
||||
* all slots are allocated
|
||||
*/
|
||||
void *uc_slot_alloc(UCSlotAlloc *sa);
|
||||
|
||||
|
||||
/** Free a memory slot previously returned from uc_slot_alloc()
|
||||
*
|
||||
* @param sa The allocator to free from.
|
||||
* @param slot The memory slot to free. Passing a NULL pointer is undefined behavior
|
||||
*/
|
||||
void uc_slot_free(UCSlotAlloc *restrict sa, void *restrict slot);
|
||||
|
||||
/** uc_slot_free() where the slot can be a NULL pointer */
|
||||
#define uc_slot_free_safe(sa, slot) \
|
||||
do { \
|
||||
if ((slot) != NULL) { \
|
||||
uc_slot_free((sa), (slot)); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,60 +0,0 @@
|
||||
#ifndef UC_SPOOL_H_
|
||||
#define UC_SPOOL_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @file
|
||||
* Static pool allocator.
|
||||
*
|
||||
* An UCPool allocates fixed size memory chunks from a provided buffer,
|
||||
* and memory allocations can be free'd in any order.
|
||||
*
|
||||
* The backing array should be suitable aligned to the types
|
||||
* that will be allocated from it. Use .e.g
|
||||
* @code
|
||||
* _Alignas(16) char mem[1024];
|
||||
* @endcode
|
||||
* as the backing buffer.
|
||||
*
|
||||
*/
|
||||
typedef struct UCPool UCPool;
|
||||
struct UCPool {
|
||||
unsigned char *free_list; // intrusive free list
|
||||
size_t block_size; // aligned size of each block
|
||||
size_t capacity; // number of blocks
|
||||
size_t align; // alignment
|
||||
unsigned char *start; // start of user buffer
|
||||
unsigned char *end; // one past end of user buffer
|
||||
};
|
||||
|
||||
/** Initialize the pool.
|
||||
*
|
||||
* Block size will be rounded up by alignment, or to size of a pointer
|
||||
*
|
||||
* @param buffer backing buffer for the pool
|
||||
* @param buffer_size size of @buffer
|
||||
* @param block_size size of each allocation.
|
||||
* @param alignment alignment of each block (must be power of 2)
|
||||
*/
|
||||
void uc_pool_init(UCPool *pool,
|
||||
void *buffer,
|
||||
size_t buffer_size,
|
||||
size_t block_size,
|
||||
size_t alignment);
|
||||
|
||||
// Allocate a block from the pool. Returns NULL if no more space
|
||||
void *uc_pool_alloc(UCPool *pool);
|
||||
|
||||
// Free @p . Returns memory to the pool
|
||||
void uc_pool_free(UCPool *pool, void *p);
|
||||
void uc_pool_reset(UCPool *pool);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,25 +0,0 @@
|
||||
import os
|
||||
|
||||
Import('env', 'build_type', 'prefix', 'lib_suffix', 'subst_info')
|
||||
|
||||
ucore_env = env.Clone()
|
||||
|
||||
ucore_env.Substfile('version.c.in', SUBST_DICT = subst_info)
|
||||
|
||||
sources = ucore_env.Glob('*.c')
|
||||
|
||||
#print 'ucore_env', dir(ucore_env)
|
||||
#print ucore_env.Dump()
|
||||
#print 'sources:', sources[len(sources)-2]
|
||||
|
||||
#Build library
|
||||
ucore_staticlib = ucore_env.StaticLibrary(target='ucore' + lib_suffix , source=sources)
|
||||
ucore_sharedlib = ucore_env.SharedLibrary(target='ucore' + lib_suffix , source=sources)
|
||||
|
||||
#install targets
|
||||
ucore_env.Alias('install',
|
||||
ucore_env.Install(os.path.join(prefix, 'lib'), [ucore_staticlib]))
|
||||
ucore_env.Alias('install',
|
||||
ucore_env.InstallVersionedLib(os.path.join(prefix, 'lib'), [ucore_sharedlib]))
|
||||
|
||||
ucore_env.Default([ucore_staticlib, ucore_sharedlib])
|
||||
@@ -1,30 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include "ucore/sarena.h"
|
||||
|
||||
void *uc_sar_alloc(UCSArena *a, size_t sz)
|
||||
{
|
||||
if (a->curr + sz > a->end) {
|
||||
return NULL;
|
||||
}
|
||||
void *start = a->curr;
|
||||
a->curr += sz;
|
||||
return start;
|
||||
}
|
||||
|
||||
void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align)
|
||||
{
|
||||
assert(align != 0);
|
||||
assert((align & (align - 1)) == 0); //power of 2
|
||||
|
||||
uintptr_t curr = (uintptr_t)a->curr;
|
||||
uintptr_t aligned = (curr + (align - (uintptr_t)1)) & ~(align - (uintptr_t)1);
|
||||
unsigned char *start = (unsigned char *)aligned;
|
||||
|
||||
if (start + sz > a->end) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
a->curr = start + sz;
|
||||
|
||||
return start;
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include "ucore/bitvec.h"
|
||||
#include "ucore/slot_allocator.h"
|
||||
|
||||
void *uc_slot_alloc(UCSlotAlloc *sa)
|
||||
{
|
||||
int free_slot = uc_bv_find_first_zero(&sa->bitmap);
|
||||
if (free_slot < 0) {
|
||||
return NULL;
|
||||
} else if ((size_t)free_slot >= sa->num_slots) { // bit vector may have more bits than slots
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uc_bv_set_bit(&sa->bitmap, free_slot);
|
||||
|
||||
return sa->slots + free_slot * sa->slot_size;
|
||||
}
|
||||
|
||||
void uc_slot_free(UCSlotAlloc *restrict sa, void *restrict slot)
|
||||
{
|
||||
assert((uint8_t *)slot >= sa->slots);
|
||||
int slot_index = ((uint8_t *)slot - sa->slots) / sa->slot_size;
|
||||
assert((size_t)slot_index < sa->num_slots);
|
||||
uc_bv_clr_bit(&sa->bitmap, slot_index);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
UC_SLOT_ALLOC_DEF(sa, 24, 1)
|
||||
|
||||
void *p1 = uc_slot_alloc(&sa);
|
||||
printf("%p\n", &sa_memory.memory[0]);
|
||||
printf("%p\n", p1);
|
||||
|
||||
void *p2 = uc_slot_alloc(&sa);
|
||||
printf("%p\n", p2);
|
||||
uc_slot_free(&sa, p1);
|
||||
|
||||
p2 = uc_slot_alloc(&sa);
|
||||
printf("%p\n", p2);
|
||||
|
||||
printf("__malloc__\n");
|
||||
void *v = malloc(24 * (1<<16));
|
||||
UC_SLOT_ALLOC_DEF_EX(sa2, 24, (1<<16), v)
|
||||
char *v2 = NULL;
|
||||
for (int i = 0; i < 1<<16; i++) {
|
||||
char *c = uc_slot_alloc(&sa2);
|
||||
if (c == NULL) {
|
||||
printf("uc_slot_alloc failed at %d\n", i);
|
||||
break;
|
||||
}
|
||||
*c = i;
|
||||
if (i == 0) {
|
||||
v2 = c;
|
||||
}
|
||||
}
|
||||
for (int i = (1<<16) -1 ; i >= 0; i--) {
|
||||
uc_slot_free(&sa2, v2 + 24*i);
|
||||
}
|
||||
free(v);
|
||||
return 0;
|
||||
}
|
||||
|
||||
-111
@@ -1,111 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include "ucore/spool.h"
|
||||
#include "ucore/utils.h"
|
||||
#include <string.h>
|
||||
|
||||
void uc_pool_init(UCPool *pool, void *buffer, size_t buffer_size, size_t block_size, size_t align)
|
||||
{
|
||||
assert(align > 0);
|
||||
assert((align & (align - 1)) == 0); // power of 2
|
||||
|
||||
|
||||
// enforce minimum for storing free list pointers
|
||||
|
||||
if (align < _Alignof(unsigned char *)) {
|
||||
align = _Alignof(unsigned char *);
|
||||
}
|
||||
|
||||
if (block_size < sizeof(unsigned char *)) {
|
||||
block_size = sizeof(unsigned char *);
|
||||
}
|
||||
// ensure initial alignment
|
||||
size_t stride = UC_ALIGN(block_size, align);
|
||||
|
||||
// align first usable block
|
||||
uintptr_t start_addr = (uintptr_t)buffer;
|
||||
uintptr_t first_addr = UC_ALIGN(start_addr, align);
|
||||
size_t padding = first_addr - start_addr;
|
||||
|
||||
if (padding + stride > buffer_size) {
|
||||
pool->capacity = 0;
|
||||
pool->free_list = NULL;
|
||||
} else {
|
||||
size_t usable = buffer_size - padding;
|
||||
pool->capacity = usable / stride;
|
||||
|
||||
// build intrusive free list
|
||||
unsigned char *p = (unsigned char *)first_addr;
|
||||
for (size_t i = 0; i < pool->capacity - 1; ++i) {
|
||||
*(unsigned char **)p = p + stride;
|
||||
p += stride;
|
||||
}
|
||||
*(unsigned char **)p = NULL; // last element
|
||||
pool->free_list = (unsigned char *)first_addr;
|
||||
}
|
||||
|
||||
pool->block_size = stride;
|
||||
pool->align = align;
|
||||
pool->start = (unsigned char *)buffer;
|
||||
pool->end = (unsigned char *)buffer + buffer_size;
|
||||
}
|
||||
|
||||
|
||||
void *uc_pool_alloc(UCPool *pool)
|
||||
{
|
||||
if (!pool->free_list) {
|
||||
return NULL;
|
||||
}
|
||||
unsigned char *block = pool->free_list;
|
||||
pool->free_list = *(unsigned char **)block;
|
||||
|
||||
#ifdef DEBUG
|
||||
memset(block, 0xCB, pool->block_size);
|
||||
#endif
|
||||
return block;
|
||||
}
|
||||
#include <stdio.h>
|
||||
|
||||
void uc_pool_free(UCPool *pool, void *p)
|
||||
{
|
||||
if (p == NULL) {
|
||||
return;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
assert((unsigned char *)p >= pool->start && (unsigned char*)p < pool->end);
|
||||
assert(((uintptr_t)p - (uintptr_t)pool->start) % pool->block_size == 0);
|
||||
|
||||
// double-free detection
|
||||
for (unsigned char *f = pool->free_list; f; f = *(unsigned char**)f) {
|
||||
if (f == p) {
|
||||
assert(0 && "Double free detected in pool!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
memset(p, 0xDD, pool->block_size);
|
||||
#endif
|
||||
// When not in use, store pointer to next free element inline in the block
|
||||
*(unsigned char **)p = pool->free_list;
|
||||
pool->free_list = p;
|
||||
}
|
||||
|
||||
|
||||
void uc_pool_reset(UCPool *pool)
|
||||
{
|
||||
if (pool->capacity == 0)
|
||||
return;
|
||||
|
||||
uintptr_t start_addr = (uintptr_t)pool->start;
|
||||
uintptr_t first_addr = UC_ALIGN(start_addr, pool->align);
|
||||
|
||||
unsigned char *p = (unsigned char *)first_addr;
|
||||
// build intrusive free list
|
||||
for (size_t i = 0; i < pool->capacity - 1; ++i) {
|
||||
*(unsigned char **)p = p + pool->block_size;
|
||||
p += pool->block_size;
|
||||
}
|
||||
|
||||
*(unsigned char **)p = NULL; // last element
|
||||
|
||||
pool->free_list = (unsigned char *)first_addr;
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
file(GLOB TEST_SOURCE_FILES test_*.c)
|
||||
|
||||
link_libraries(check ucore Threads::Threads)
|
||||
link_libraries(ucore Threads::Threads PkgConfig::CHECK)
|
||||
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
||||
include_directories("/opt/homebrew/include")
|
||||
link_directories("/opt/homebrew/lib")
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
import platform
|
||||
|
||||
Import('env', 'build_type', 'prefix', 'lib_suffix', 'version_info', 'test_xml')
|
||||
|
||||
test_env = env.Clone()
|
||||
test_env.Append(LIBPATH = ['#build/src']);
|
||||
test_env.Append(LIBS = ['ucore' + lib_suffix, 'check','m'])
|
||||
|
||||
system = platform.system()
|
||||
|
||||
#NetBSD needs /usr/pkg/...
|
||||
if 'netbsd' in system.lower():
|
||||
test_env.Append(LIBPATH = ['/usr/pkg/lib'])
|
||||
test_env.Append(LINKFLAGS = ['-Wl,-rpath,/usr/pkg/lib'])
|
||||
test_env.Append(CPPPATH = ['/usr/pkg/include'])
|
||||
|
||||
if 'darwin' in system.lower():
|
||||
test_env.Append(LIBPATH = ['/opt/homebrew/lib'])
|
||||
test_env.Append(LINKFLAGS = ['-Wl,-rpath,/opt/homebrew/lib'])
|
||||
test_env.Append(CPPPATH = ['/opt/homebrew/include'])
|
||||
|
||||
if 'darwin' not in system.lower():
|
||||
test_env.Append(LIBS = ['rt'])
|
||||
|
||||
|
||||
tests = test_env.Glob('test_*.c')
|
||||
|
||||
test_executable = 'test_runner'
|
||||
#run test_runner --xml to produce an xml result file
|
||||
if test_xml:
|
||||
test_executable += ' -x'
|
||||
|
||||
#rpath to shared lib, so test_runner can actually be executed
|
||||
test_env.Append(LINKFLAGS = ['-Wl,-rpath,\\$$ORIGIN/../src'])
|
||||
|
||||
prog = test_env.Program('test_runner', tests)
|
||||
cmd = test_env.Command(target='test_phony' ,
|
||||
source = None,
|
||||
action= test_env.Dir('.').abspath + '/' + test_executable)
|
||||
test_env.Depends(cmd, prog)
|
||||
test_env.AlwaysBuild(cmd)
|
||||
test_env.Default(test_executable)
|
||||
Reference in New Issue
Block a user