initial commit
This commit is contained in:
commit
21132d5c2c
8 changed files with 166 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
.vscode
|
||||||
|
build
|
||||||
18
CMakeLists.txt
Normal file
18
CMakeLists.txt
Normal file
|
|
@ -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)
|
||||||
5
cutil.c
Normal file
5
cutil.c
Normal file
|
|
@ -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;
|
||||||
15
cutil.h
Normal file
15
cutil.h
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
#ifndef __CUTIL_H
|
||||||
|
#define __CUTIL_H
|
||||||
|
|
||||||
|
#include "cutypes.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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
|
||||||
12
cutypes.h
Normal file
12
cutypes.h
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef __CUTYPES_H
|
||||||
|
#define __CUTYPES_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef unsigned int uint;
|
||||||
|
|
||||||
|
typedef void* cu_ptr;
|
||||||
|
#define CU_PTR_SIZE sizeof(cu_ptr)
|
||||||
|
|
||||||
|
#endif
|
||||||
57
list.c
Normal file
57
list.c
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
#include "list.h"
|
||||||
|
#include "cutil.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
32
list.h
Normal file
32
list.h
Normal file
|
|
@ -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
|
||||||
25
test.c
Normal file
25
test.c
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue