Skip to content

Commit

Permalink
add CMakeLists.txt (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
skiars authored Dec 28, 2023
1 parent e26caec commit 97e0fca
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ __pycache__
.settings/
docs/build/html/
docs/_doxygen/xml/
build/
cmake-build-*
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.10)

project(Berry C)
set(CMAKE_C_STANDARD 99)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(BERRY_COC ${CMAKE_CURRENT_SOURCE_DIR}/tools/coc/coc)
set(BERRY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(BERRY_CONFIG_DIR default CACHE FILEPATH "The directory of berry_conf.h.")
set(BERRY_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/${BERRY_CONFIG_DIR}/berry_conf.h)
set(BERRY_GENERATE ${CMAKE_CURRENT_SOURCE_DIR}/generate)

if (${CMAKE_HOST_WIN32})
set(BERRY_COC python ${BERRY_COC})
endif ()

file(MAKE_DIRECTORY generate)

# berry library
file(GLOB SOURCES src/*.c)
add_library(libberry ${SOURCES})
target_include_directories(libberry PUBLIC src ${BERRY_CONFIG_DIR})

add_custom_target(berry-coc
COMMAND ${BERRY_COC} -o ${BERRY_GENERATE} ${BERRY_SOURCE_DIR} -c ${BERRY_CONFIG}
DEPENDS ${SOURCES} COMMAND_EXPAND_LISTS
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Generate coc objects"
)
add_dependencies(libberry berry-coc)

# berry default exe
file(GLOB SOURCES_EXE default/*.c)
add_executable(berry ${SOURCES_EXE})
target_link_libraries(berry PUBLIC libberry)
8 changes: 4 additions & 4 deletions default/berry.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,18 @@ static void berry_paths(bvm * vm)
}
}

static void berry_custom_paths(bvm * vm, const char *modulepath)
static void berry_custom_paths(bvm *vm, const char *modulepath)
{
const char delim[] = PATH_SEPARATOR;
char copy[strlen(modulepath)+1];
char *copy = malloc(strlen(modulepath) + 1);
strcpy(copy, modulepath);

char *ptr = strtok(copy, delim);

while (ptr != NULL) {
be_module_path_set(vm, ptr);
ptr = strtok(NULL, delim);
}
}
free(copy);
}

/*
Expand Down
69 changes: 37 additions & 32 deletions src/be_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,43 @@
#define realloc BE_EXPLICIT_REALLOC
#endif

static void* malloc_from_pool(bvm *vm, size_t size);
static void free_from_pool(bvm *vm, void* ptr, size_t old_size);

#define POOL16_SIZE 16
#define POOL32_SIZE 32

#ifdef _MSC_VER
#include <intrin.h>
#pragma intrinsic(_BitScanForward)
#endif

#if defined(__GNUC__)
#define popcount(v) __builtin_popcount(v)
#define ffs(v) __builtin_ffs(v)
#elif defined(_MSC_VER)
#define popcount(v) __popcnt(v)

static int ffs(unsigned x)
{
unsigned long i;
return _BitScanForward(&i, x) ? i : 0;
}
#else
/* https://github.com/hcs0/Hackers-Delight/blob/master/pop.c.txt - count number of 1-bits */
static int popcount(uint32_t n)
{
n = (n & 0x55555555u) + ((n >> 1) & 0x55555555u);
n = (n & 0x33333333u) + ((n >> 2) & 0x33333333u);
n = (n & 0x0f0f0f0fu) + ((n >> 4) & 0x0f0f0f0fu);
n = (n & 0x00ff00ffu) + ((n >> 8) & 0x00ff00ffu);
n = (n & 0x0000ffffu) + ((n >>16) & 0x0000ffffu);
return n;
}

#error "unsupport compiler for ffs()"
#endif

static void* malloc_from_pool(bvm *vm, size_t size);
static void free_from_pool(bvm *vm, void* ptr, size_t old_size);

BERRY_API void* be_os_malloc(size_t size)
{
return malloc(size);
Expand Down Expand Up @@ -66,7 +97,7 @@ BERRY_API void* be_realloc(bvm *vm, void *ptr, size_t old_size, size_t new_size)
if (!ptr || (old_size == 0)) {
block = malloc_from_pool(vm, new_size);
}

/* Case 2: deallocate */
else if (new_size == 0) {
#if BE_USE_PERF_COUNTERS
Expand Down Expand Up @@ -176,11 +207,7 @@ static void* malloc_from_pool(bvm *vm, size_t size) {
/* look for an empty slot */
if (pool16->bitmap != 0x0000) {
/* there is a free slot */
#ifdef __GNUC__
int bit = __builtin_ffs(pool16->bitmap) - 1;
#else
int bit = ffs(pool16->bitmap) - 1;
#endif
if (bit >= 0) {
/* we found a free slot */
// bitClear(pool16->bitmap, bit);
Expand Down Expand Up @@ -208,11 +235,7 @@ static void* malloc_from_pool(bvm *vm, size_t size) {
/* look for an empty slot */
if (pool32->bitmap != 0x0000) {
/* there is a free slot */
#ifdef __GNUC__
int bit = __builtin_ffs(pool32->bitmap) - 1;
#else
int bit = ffs(pool32->bitmap) - 1;
#endif
if (bit >= 0) {
/* we found a free slot */
// bitClear(pool32->bitmap, bit);
Expand Down Expand Up @@ -327,39 +350,21 @@ BERRY_API void be_gc_free_memory_pools(bvm *vm) {
vm->gc.pool32 = NULL;
}

/* https://github.com/hcs0/Hackers-Delight/blob/master/pop.c.txt - count number of 1-bits */
static int pop0(uint32_t n) __attribute__((unused));
static int pop0(uint32_t n) {
n = (n & 0x55555555u) + ((n >> 1) & 0x55555555u);
n = (n & 0x33333333u) + ((n >> 2) & 0x33333333u);
n = (n & 0x0f0f0f0fu) + ((n >> 4) & 0x0f0f0f0fu);
n = (n & 0x00ff00ffu) + ((n >> 8) & 0x00ff00ffu);
n = (n & 0x0000ffffu) + ((n >>16) & 0x0000ffffu);
return n;
}

#ifdef __GNUC__
#define count_bits_1(v) __builtin_popcount(v)
#else
#define count_bits_1(v) pop0(v)
#endif


BERRY_API void be_gc_memory_pools_info(bvm *vm, size_t* slots_used, size_t* slots_allocated) {
size_t used = 0;
size_t allocated = 0;

gc16_t* pool16 = vm->gc.pool16;
while (pool16) {
allocated += POOL16_SLOTS;
used += POOL16_SLOTS - count_bits_1(pool16->bitmap);
used += POOL16_SLOTS - popcount(pool16->bitmap);
pool16 = pool16->next;
}

gc32_t* pool32 = vm->gc.pool32;
while (pool32) {
allocated += POOL32_SLOTS;
used += POOL32_SLOTS - count_bits_1(pool32->bitmap);
used += POOL32_SLOTS - popcount(pool32->bitmap);
pool32 = pool32->next;
}
if (slots_used) { *slots_used = used; }
Expand Down

0 comments on commit 97e0fca

Please sign in to comment.