From 21132d5c2ca73da87fcaaf0f0520440e75268e24 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sat, 16 Apr 2022 16:22:31 +0300 Subject: [PATCH] initial commit --- .gitignore | 2 ++ CMakeLists.txt | 18 ++++++++++++++++ cutil.c | 5 +++++ cutil.h | 15 +++++++++++++ cutypes.h | 12 +++++++++++ list.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ list.h | 32 ++++++++++++++++++++++++++++ test.c | 25 ++++++++++++++++++++++ 8 files changed, 166 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 cutil.c create mode 100644 cutil.h create mode 100644 cutypes.h create mode 100644 list.c create mode 100644 list.h create mode 100644 test.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af96791 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c124fff --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.10) +project(cutil) + +set(HEADERS + cutypes.h + cutil.h + list.h +) + +set(SOURCES + cutil.c + list.c +) + +add_library(cutil STATIC ${SOURCES} ${HEADERS}) + +add_executable(test test.c) +target_link_libraries(test cutil) \ No newline at end of file diff --git a/cutil.c b/cutil.c new file mode 100644 index 0000000..b5cd90e --- /dev/null +++ b/cutil.c @@ -0,0 +1,5 @@ +#include "cutil.h" + +void* (*cu_malloc)(size_t) = CUTIL_MALLOC; +void* (*cu_relloac)(void*, size_t) = CUTIL_REALLOC; +void* (*cu_free)(void*) = CUTIL_FREE; diff --git a/cutil.h b/cutil.h new file mode 100644 index 0000000..0eff4f2 --- /dev/null +++ b/cutil.h @@ -0,0 +1,15 @@ +#ifndef __CUTIL_H +#define __CUTIL_H + +#include "cutypes.h" +#include + +#define CUTIL_MALLOC malloc +#define CUTIL_REALLOC realloc +#define CUTIL_FREE free + +extern void* (*cu_malloc)(size_t); +extern void* (*cu_relloac)(void*, size_t); +extern void* (*cu_free)(void*); + +#endif \ No newline at end of file diff --git a/cutypes.h b/cutypes.h new file mode 100644 index 0000000..4b34de4 --- /dev/null +++ b/cutypes.h @@ -0,0 +1,12 @@ +#ifndef __CUTYPES_H +#define __CUTYPES_H + +#include +#include + +typedef unsigned int uint; + +typedef void* cu_ptr; +#define CU_PTR_SIZE sizeof(cu_ptr) + +#endif \ No newline at end of file diff --git a/list.c b/list.c new file mode 100644 index 0000000..1e2e420 --- /dev/null +++ b/list.c @@ -0,0 +1,57 @@ +#include "list.h" +#include "cutil.h" +#include + +void list_init(list_t* list) +{ + list->first = NULL; + list->last = NULL; + list->count = 0; +} + +void list_clear(list_t* list) +{ + list_node_t* node = list->first; + if (node) + { + list_node_t* next; + do { + next = node->next; + cu_free(node); + } while(node = next); + } + list->first = NULL; + list->last = NULL; + list->count = 0; +} + +void list_add(list_t* list, uint size, void* data) +{ + uint nodeSize = LIST_NODE_SIZE + size; + list_node_t* node = (list_node_t*)cu_malloc(nodeSize); + memset(node, '\0', nodeSize); + if (data) + { + memcpy((uint8_t*)node + LIST_NODE_SIZE, data, size); + } + + list_node_t* last = list->last; + if (!list->first) list->first = node; + if (last) + { + last->next = node; + node->prev = last; + } + list->last = node; + list->count++; +} + +void list_remove(list_t* list, list_node_t* node) +{ + if (node->prev) node->prev->next = node->next; + if (node->next) node->next->prev = node->prev; + if (list->first == node) list->first = node->next; + if (list->last == node) list->last == node->prev; + list->count--; + cu_free(node); +} diff --git a/list.h b/list.h new file mode 100644 index 0000000..955a13f --- /dev/null +++ b/list.h @@ -0,0 +1,32 @@ +#ifndef __LIST_H +#define __LIST_H + +#include "cutypes.h" + +typedef struct list_node_s { + struct list_node_s* prev; + struct list_node_s* next; + uint8_t data[1]; +} list_node_t; + +#define LIST_NODE_SIZE (CU_PTR_SIZE * 2) +#define LIST_NODE_PTR(data) ((uint8_t*)data - LIST_NODE_SIZE) +#define LIST_NODE_DATA(node) ((uint8_t*)node + LIST_NODE_SIZE) +#define LIST_NODE_VALUE(node, type) (*(type*)LIST_NODE_DATA(node)) + +typedef struct { + list_node_t* first; + list_node_t* last; + uint count; +} list_t; + +void list_init(list_t* list); +void list_clear(list_t* list); + +void list_add(list_t* list, uint size, void* data); +void list_remove(list_t* list, list_node_t* node); + +#define CU_LIST_FOREACH(list) \ + for(list_node_t* i = (list)->first; i; i = i->next) + +#endif \ No newline at end of file diff --git a/test.c b/test.c new file mode 100644 index 0000000..567455c --- /dev/null +++ b/test.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include "list.h" + +int main() +{ + list_t list; + list_init(&list); + + srand(time(NULL)); + for (uint i = 0; i < 5; i++) + { + uint num = rand(); + list_add(&list, sizeof(uint), &num); + } + + CU_LIST_FOREACH(&list) + { + printf("%p\t%d\n", i, LIST_NODE_VALUE(i, uint)); + } + + list_clear(&list); + return 0; +} \ No newline at end of file