Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ DMOD_INC_DIRS = include\
build/_deps/dmod-build
DMOD_LIBS = dmod_inc
DMOD_GEN_HEADERS_IN =
DMOD_DEFINITIONS = DMLIST_VERSION_STRING="== dmlist ver. 1.0 ==\n"
DMOD_DEFINITIONS = DMLIST_VERSION_STRING="== dmlist ver. 1.0 ==\n" DMOD_CURRENT_ALLOCATOR="dmlist"

# -----------------------------------------------------------------------------
# Initialization of paths
Expand Down
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ All simple tests completed!

int main(void) {
// 1. Create a list
dmlist_context_t* list = dmlist_create("main");
dmlist_context_t* list = dmlist_create();
if (list == NULL) {
return -1;
}
Expand Down Expand Up @@ -292,7 +292,7 @@ int main(void) {
#include <string.h>

void string_list_example(void) {
dmlist_context_t* list = dmlist_create("string_module");
dmlist_context_t* list = dmlist_create();

// Add strings to the list
dmlist_push_back(list, "Hello");
Expand Down Expand Up @@ -329,7 +329,7 @@ int compare_ints(const void* a, const void* b) {
}

void find_example(void) {
dmlist_context_t* list = dmlist_create("search_module");
dmlist_context_t* list = dmlist_create();

// Add some values
int values[] = {10, 20, 30, 40, 50};
Expand Down Expand Up @@ -370,7 +370,7 @@ bool sum_accumulator(void* data, void* user_data) {
}

void iteration_example(void) {
dmlist_context_t* list = dmlist_create("iter_module");
dmlist_context_t* list = dmlist_create();

// Add values
int values[] = {1, 2, 3, 4, 5};
Expand Down Expand Up @@ -399,7 +399,7 @@ void iteration_example(void) {
#include <stdio.h>

void insert_example(void) {
dmlist_context_t* list = dmlist_create("insert_module");
dmlist_context_t* list = dmlist_create();

int values[] = {1, 2, 4, 5};

Expand Down Expand Up @@ -435,7 +435,7 @@ int compare_strings(const void* a, const void* b) {
}

void remove_example(void) {
dmlist_context_t* list = dmlist_create("remove_module");
dmlist_context_t* list = dmlist_create();

// Add elements
dmlist_push_back(list, "apple");
Expand Down Expand Up @@ -482,7 +482,7 @@ bool print_student(void* data, void* user_data) {
}

void struct_example(void) {
dmlist_context_t* list = dmlist_create("student_module");
dmlist_context_t* list = dmlist_create();

// Create students
Student s1 = {"Alice", 20, 85.5};
Expand Down Expand Up @@ -516,15 +516,28 @@ void struct_example(void) {
#### `dmlist_create`

```c
dmlist_context_t* dmlist_create(const char* module_name);
dmlist_context_t* dmlist_create(void);
```

Create a linked list context.
Create a linked list context, attributing its allocations to the caller's own
allocator (`DMOD_CURRENT_ALLOCATOR` - see `dmod_sal.h`). This is a macro around
`dmlist_create_ex()` and is what you want in almost all cases.

- **Returns:** Pointer to the list context, or `NULL` if creation fails
- **Note:** All memory allocations for this list will be tracked under the caller's own name

#### `dmlist_create_ex`

```c
dmlist_context_t* dmlist_create_ex(const char* module_name);
```

Create a linked list context, attributing its allocations to an explicit owner
instead of the caller.

- **Parameters:**
- `module_name`: Name of the module using the list (for memory tracking)
- `module_name`: Name to track this list's memory allocations under
- **Returns:** Pointer to the list context, or `NULL` if creation fails
- **Note:** All memory allocations for this list will be tracked under `module_name`

#### `dmlist_destroy`

Expand Down Expand Up @@ -806,7 +819,7 @@ int compare_priority(const void* a, const void* b) {
}

void task_queue_example(void) {
dmlist_context_t* queue = dmlist_create("task_queue");
dmlist_context_t* queue = dmlist_create();

// Add tasks
Task t1 = {1, "Low priority task"};
Expand Down Expand Up @@ -843,7 +856,7 @@ typedef struct {

History* history_create(void) {
History* hist = (History*)Dmod_Malloc(sizeof(History), "history");
hist->list = dmlist_create("history");
hist->list = dmlist_create();
hist->max_size = MAX_HISTORY;
return hist;
}
Expand Down
18 changes: 14 additions & 4 deletions include/dmlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ typedef bool (*dmlist_iterator_func_t)(void* data, void* user_data);
typedef int (*dmlist_compare_func_t)(const void* data1, const void* data2);

/**
* @brief Create a linked list context.
*
* @brief Create a linked list context, attributing its allocations to an explicit owner.
*
* @param module_name Name of the module using the list (for memory tracking).
*
*
* @return Pointer to the list context, or NULL if creation fails.
*/
DMOD_BUILTIN_API( dmlist, 1.0, dmlist_context_t*, _create, ( const char* module_name ) );
DMOD_BUILTIN_API( dmlist, 1.0, dmlist_context_t*, _create_ex, ( const char* module_name ) );

/**
* @brief Create a linked list context, allocating on the caller's own allocator.
*
* Forwards to dmlist_create_ex() with DMOD_CURRENT_ALLOCATOR (see dmod_sal.h) - the
* identity of whoever is calling this macro, not dmlist itself - so every caller's
* list nodes are tracked, and bulk-freed on unload, under the caller's own name
* rather than under "dmlist".
*/
#define dmlist_create() dmlist_create_ex( DMOD_CURRENT_ALLOCATOR )

/**
* @brief Destroy a linked list and free all its nodes.
Expand Down
4 changes: 2 additions & 2 deletions src/dmlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ static void free_node( dmlist_node_t* node )
}
}

DMOD_INPUT_API_DECLARATION( dmlist, 1.0, dmlist_context_t*, _create, ( const char* module_name ) )
DMOD_INPUT_API_DECLARATION( dmlist, 1.0, dmlist_context_t*, _create_ex, ( const char* module_name ) )
{
if( module_name == NULL )
{
DMOD_LOG_ERROR("dmlist: _create called with NULL module_name.\n");
DMOD_LOG_ERROR("dmlist: _create_ex called with NULL module_name.\n");
return NULL;
}

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# Create a simple test executable
add_executable(test_simple test_simple.c dmod_stubs.c)
dmod_set_current_allocator(test_simple)
target_link_libraries(test_simple PRIVATE dmlist dmod_system)

# Add test
Expand Down
2 changes: 1 addition & 1 deletion tests/test_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(void) {

// Test 1: Create
printf("Create: ");
dmlist_context_t* list = dmlist_create("test");
dmlist_context_t* list = dmlist_create();
if(list == NULL) {
TEST_FAIL();
return 1;
Expand Down