commit 0b91fa04378ec4b5c7f6bcdc8090a2f7304fb097
parent fca55fc9e22305d8e1f98cb57e203b4004c54d37
Author: Dominik Schmidt <das1993@hotmail.com>
Date: Thu, 11 Sep 2014 11:46:06 +0200
SAlloc and Array functions added
Diffstat:
4 files changed, 130 insertions(+), 0 deletions(-)
diff --git a/include/Util/Array.h b/include/Util/Array.h
@@ -0,0 +1,16 @@
+#pragma once
+#include <EGong/Util/SAlloc.h>
+
+
+struct Array{
+ struct SAlloc memory;
+ unsigned int element_count;
+ size_t element_size;
+};
+
+extern void Array_init(struct Array *array, size_t element_size, size_t chunks);
+extern int Array_add(struct Array *array, void *element);
+extern void *Array_get(struct Array *array, unsigned int id);
+extern int Array_delete(struct Array *array, unsigned int id);
+extern void Array_destroy(struct Array *array);
+extern void Array_foreach(struct Array *array, void(*func)(void*));
diff --git a/include/Util/SAlloc.h b/include/Util/SAlloc.h
@@ -0,0 +1,17 @@
+#pragma once
+#include <stdint.h>
+#include <stddef.h>
+
+#define EGONG_SALLOC_DEFAULT_CHUNK 500
+
+struct SAlloc{
+ void *data;
+ size_t m_allocd;
+ size_t s_allocd;
+ size_t chunk_size;
+};
+
+extern void SAlloc_init(struct SAlloc *salloc, size_t chunk);
+extern void *SAlloc_alloc(struct SAlloc *salloc, size_t size);
+extern int SAlloc_rewind(struct SAlloc *salloc, size_t size);
+extern void *SAlloc_destroy(struct SAlloc *salloc);
diff --git a/src/Util/Array.c b/src/Util/Array.c
@@ -0,0 +1,50 @@
+#include <EGong/Util/Array.h>
+#include <EGong/Util/Log.h>
+#include <EGong/Util/SAlloc.h>
+#include <stdint.h>
+#include <string.h>
+
+void Array_init(struct Array *array, size_t element_size, size_t chunks){
+ if(chunks==0)chunks=10;
+ SAlloc_init(&array->memory, element_size*chunks);
+ array->element_size=element_size;
+ array->element_count=0;
+}
+
+int Array_add(struct Array *array, void *element){
+ void *ptr=SAlloc_alloc(&array->memory, array->element_size);
+ memcpy(ptr, element, array->element_size);
+ array->element_count++;
+}
+void *Array_get(struct Array *array, unsigned int id){
+ if(id>array->element_count-1){
+ do_log("Element out of boundary", LOG_TYPE_NORMAL, LOG_LEVEL_ERROR);
+ return NULL;
+ }
+ return (void *)((intptr_t)array->memory.data+(uintptr_t)(id*array->element_size));
+}
+int Array_delete(struct Array *array, unsigned int id){
+ if(id>array->element_count-1){
+ do_log("Element out of boundary", LOG_TYPE_NORMAL, LOG_LEVEL_ERROR);
+ return -1;
+ }
+ else{
+ if(id<array->element_count-1){
+ void *ptr=Array_get(array, id);
+ memcpy(ptr, ptr+array->element_size, array->element_size*(array->element_count-id));
+ }
+ SAlloc_rewind(&array->memory, array->element_size);
+ }
+ array->element_count--;
+ return 0;
+}
+
+void Array_foreach(struct Array *array, void(*func)(void*)){
+ unsigned int i;
+ for(i=0; i<array->element_count; i++){
+ func(Array_get(array,i));
+ }
+}
+void Array_destroy(struct Array *array){
+ SAlloc_destroy(&array->memory);
+}
diff --git a/src/Util/SAlloc.c b/src/Util/SAlloc.c
@@ -0,0 +1,47 @@
+#include <stdlib.h>
+#include <EGong/Util/SAlloc.h>
+#include <EGong/Util/Log.h>
+
+void SAlloc_init(struct SAlloc *salloc, size_t chunk){
+ if(chunk==0){
+ salloc->chunk_size=EGONG_SALLOC_DEFAULT_CHUNK;
+ }
+ else{
+ salloc->chunk_size=chunk;
+ }
+ salloc->m_allocd=salloc->s_allocd=0;
+ salloc->data=NULL;
+}
+
+void *SAlloc_alloc(struct SAlloc *salloc, size_t size){
+ salloc->s_allocd+=size;
+ while(salloc->s_allocd>salloc->m_allocd){
+ salloc->m_allocd+=salloc->chunk_size;
+ }
+ salloc->data=realloc(salloc->data, salloc->chunk_size);
+ if(salloc->data==NULL){
+ do_log("Couldn't allocate memory", LOG_TYPE_SIGNAL, LOG_LEVEL_ERROR);
+ return NULL;
+ }
+ return (void *)((intptr_t)salloc->data+(intptr_t)(salloc->s_allocd-size));
+}
+int SAlloc_rewind(struct SAlloc *salloc, size_t size){
+ if(size>=salloc->m_allocd){
+ do_log("Trying to rewind out of boundary", LOG_TYPE_NORMAL, LOG_LEVEL_WARNING);
+ SAlloc_destroy(salloc);
+ SAlloc_init(salloc, salloc->chunk_size);
+ }
+ salloc->s_allocd-=size;
+ while(salloc->s_allocd<salloc->m_allocd-salloc->chunk_size){
+ salloc->m_allocd-=salloc->chunk_size;
+ }
+ salloc->data=realloc(salloc->data, salloc->chunk_size);
+ if(salloc->data==NULL){
+ do_log("Couldn't allocate memory", LOG_TYPE_SIGNAL, LOG_LEVEL_ERROR);
+ return -1;
+ }
+ return 0;
+}
+void *SAlloc_destroy(struct SAlloc *salloc){
+ free(salloc->data);
+}