aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt17
-rw-r--r--cm-demo.c96
-rw-r--r--makefile76
3 files changed, 9 insertions, 180 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2f87f41..67bb902 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,24 +12,24 @@ endif()
set(CM_PANIC_SYM "panic_impl" CACHE STRING "Symbol name of the panic method")
-option(CM_ENABLE_EXTENDED_DEBUG "Whether to enable extended debug information" OFF)
option(CM_ENABLE_UBSAN "Whether to enable undefined behavior sanitizer" OFF)
option(CM_ENABLE_ASAN "Whether to enable address sanitizer" OFF)
option(CM_ENABLE_SSE2 "Whether to enable SSE2 usage" OFF)
+option(CM_ENABLE_NATIVE "Whether to enable -march=native" OFF)
## source
-set(cheesemap_source "${CMAKE_CURRENT_SOURCE_DIR}/cheesemap.c")
+set(cm_topdir "${CMAKE_CURRENT_SOURCE_DIR}")
+set(cm_source "${cm_topdir}/cheesemap.c")
## library target
-add_library(cheesemap OBJECT ${cheesemap_source})
-target_include_directories(cheesemap PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
+add_library(cheesemap OBJECT ${cm_source})
+target_include_directories(cheesemap PUBLIC ${cm_topdir})
target_compile_definitions(cheesemap
PRIVATE
CM_PANIC_SYM=${CM_PANIC_SYM}
- $<$<BOOL:${CM_ENABLE_EXTENDED_DEBUG}>:CM_ENABLE_EXTENDED_DEBUG=1>
$<$<BOOL:${CM_ENABLE_UBSAN}>:CM_ENABLE_UBSAN=1>
$<$<BOOL:${CM_ENABLE_ASAN}>:CM_ENABLE_ASAN=1>
$<$<BOOL:${CM_ENABLE_SSE2}>:CM_ENABLE_SSE2=1>
@@ -39,24 +39,25 @@ target_compile_options(cheesemap
PRIVATE
-Wall -Wextra -Werror
- $<$<BOOL:${CM_ENABLE_EXTENDED_DEBUG}>:-g3>
+ $<$<CONFIG:Debug>:-g3>
$<$<BOOL:${CM_ENABLE_UBSAN}>:-fsanitize=undefined>
$<$<BOOL:${CM_ENABLE_ASAN}>:-fsanitize=address>
$<$<BOOL:${CM_ENABLE_SSE2}>:-msse2>
+ $<$<BOOL:${CM_ENABLE_NATIVE}>:-march=native>
)
set_target_properties(cheesemap PROPERTIES
C_STANDARD 11
C_STANDARD_REQUIRED ON
C_EXTENSIONS ON
- POSITION_INDEPENDENT_CODE ON
+ POSITION_INDEPENDENT_CODE ON
)
## configuration summary
message(STATUS "cheesemap configuration:")
message(STATUS " CM_PANIC_SYM: ${CM_PANIC_SYM}")
-message(STATUS " CM_ENABLE_EXTENDED_DEBUG: ${CM_ENABLE_EXTENDED_DEBUG}")
message(STATUS " CM_ENABLE_UBSAN: ${CM_ENABLE_UBSAN}")
message(STATUS " CM_ENABLE_ASAN: ${CM_ENABLE_ASAN}")
message(STATUS " CM_ENABLE_SSE2: ${CM_ENABLE_SSE2}")
+message(STATUS " CM_ENABLE_NATIVE: ${CM_ENABLE_NATIVE}")
diff --git a/cm-demo.c b/cm-demo.c
deleted file mode 100644
index cdcf7bb..0000000
--- a/cm-demo.c
+++ /dev/null
@@ -1,96 +0,0 @@
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "cheesemap.h"
-
-_Noreturn void panic_impl(const char* file, cm_u32 line, const char* fmt, ...) {
- fprintf(stderr, "Panic at %s:%u: ", file, line);
- va_list args;
- va_start(args, fmt);
- vfprintf(stderr, fmt, args);
- va_end(args);
- fprintf(stderr, "\n");
- abort();
-}
-
-// Convenience macro for array length
-#define countof(arr) (sizeof(arr) / sizeof(*(arr)))
-
-// Simple hash function for string keys
-cm_u64 hash_string(const cm_u8* key, cm_u8* user) {
- (void)user;
- const char* str = *(const char**)key;
- cm_u64 hash = 5381;
- int c;
- while ((c = *str++)) hash = ((hash << 5) + hash) + c; // hash * 33 + c
- return hash;
-}
-
-// Compare function for string keys
-bool compare_string(const cm_u8* key1, const cm_u8* key2, cm_u8* user) {
- (void)user;
- return strcmp(*(const char**)key1, *(const char**)key2) == 0;
-}
-
-// Default allocator (uses malloc)
-cm_u8* default_alloc(cm_usize size, cm_u8* user) {
- (void)user;
- return malloc(size);
-}
-
-// Default deallocator (uses free)
-void default_dealloc(cm_u8* ptr, cm_u8* user) {
- (void)user;
- free(ptr);
-}
-
-int main(void) {
- // Create a map: string -> int (word frequency counter)
- struct cheesemap map;
- cm_init_(&map, const char*, int, NULL, hash_string, compare_string,
- default_alloc, default_dealloc);
-
- // Count word frequencies
- const char* words[] = {"hello", "world", "hello",
- "cheesemap", "world", "hello"};
- for (size_t i = 0; i < countof(words); i++) {
- int* count;
- if (cm_lookup_(&map, words[i], &count)) {
- (*count)++; // Word exists, increment
- } else {
- int initial = 1;
- cm_insert_(&map, words[i], initial);
- }
- }
-
- // Iterate and print all word counts
- printf("Word frequencies:\n");
- struct cheesemap_iter iter;
- cm_iter_init(&iter, &map);
- const char** word;
- int* count;
- while (cm_iter_next_(&iter, &map, &word, &count)) {
- printf(" %s: %d\n", *word, *count);
- }
-
- // Lookup a specific word
- const char* search = "hello";
- if (cm_lookup_(&map, search, &count)) {
- printf("\n'%s' appears %d times\n", search, *count);
- }
-
- // Remove a word
- const char* remove = "world";
- cm_remove_(&map, remove, NULL);
- printf("Removed '%s'\n", remove);
-
- // Verify removal
- if (!cm_lookup_(&map, remove, &count)) {
- printf("'%s' no longer in map\n", remove);
- }
-
- cm_drop(&map);
- return 0;
-}
diff --git a/makefile b/makefile
deleted file mode 100644
index c1bf8f0..0000000
--- a/makefile
+++ /dev/null
@@ -1,76 +0,0 @@
-.SUFFIXES:
-.SILENT:
-
-# Build configuration options
-CM_OPT_CC_FLAGS ?=
-CM_OPT_PANIC_SYM ?= panic_impl
-CM_OPT_RELEASE ?= 1
-CM_OPT_ENABLE_UBSAN ?= 0
-CM_OPT_ENABLE_ASAN ?= 0
-CM_OPT_ENABLE_SSE2 ?= 0
-CM_OPT_STANDALONE ?= 1
-
-CC ?= gcc
-PRINTF ?= printf
-RM_FLAGS = -f
-
-# Project root directory
-CM_DIR ?= $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
-
-# Target: cm (cheesemap.o)
-cm = $(CM_DIR)/cheesemap.o
-cm_SOURCE = $(CM_DIR)/cheesemap.c
-cm_DEPEND = $(cm_SOURCE:.c=.d)
-
-cm_CFLAGS = -std=gnu11 \
- -Wall -Wextra -Werror \
- -MMD -MP -I$(CM_DIR)
-
-cm_CFLAGS += $(CM_OPT_CC_FLAGS)
-cm_CFLAGS += -DCM_PANIC_SYM='$(CM_OPT_PANIC_SYM)'
-
-ifeq ($(CM_OPT_RELEASE),1)
- cm_CFLAGS += -O2 -fno-stack-protector
-else
- cm_CFLAGS += -g3
-endif
-
-ifeq ($(CM_OPT_ENABLE_UBSAN),1)
- cm_CFLAGS += -fsanitize=undefined
-endif
-
-ifeq ($(CM_OPT_ENABLE_ASAN),1)
- cm_CFLAGS += -fsanitize=address
-endif
-
-ifeq ($(CM_OPT_ENABLE_SSE2),1)
- cm_CFLAGS += -DCM_ENABLE_SSE2=1 -msse2
-endif
-
-# Target: cm_demo
-cm_demo = $(CM_DIR)/cm-demo
-cm_demo_SOURCE = $(CM_DIR)/cm-demo.c
-cm_demo_DEPEND = $(cm_demo_SOURCE:.c=.d)
-cm_demo_CFLAGS = $(cm_CFLAGS)
-
--include $(cm_DEPEND) $(cm_demo_DEPEND)
-
-ifeq ($(CM_OPT_STANDALONE),1)
-.PHONY: all
-all: $(cm) $(cm_demo)
-endif
-
-$(cm): $(cm_SOURCE)
- @$(PRINTF) " CC %s\n" "$(notdir $@)"
- $(CC) $(cm_CFLAGS) -c $< -o $@
-
-$(cm_demo): $(cm_demo_SOURCE) $(cm)
- @$(PRINTF) " CC %s\n" "$(notdir $@)"
- $(CC) $(cm_demo_CFLAGS) $^ -o $@
-
-.PHONY: clean
-clean::
- @$(PRINTF) " RM %s\n" "$(notdir $(cm))"
- $(RM) $(RM_FLAGS) $(cm) $(cm_DEPEND)
- @$(PRINTF) " RM %s\n" "$(notdir $(cm_demo))"
- $(RM) $(RM_FLAGS) $(cm_demo) $(cm_demo_DEPEND)