diff --git a/dynarrc/common.h b/dynarrc/common.h index 7bd9c6e..f4c5c1e 100644 --- a/dynarrc/common.h +++ b/dynarrc/common.h @@ -1,6 +1,9 @@ #ifndef __dynarr_common_h__ #define __dynarr_common_h__ +// mark different regions of the code +#define _REGION_ 1 + /* for token-pasting and string concatenation */ #define MACRO_EVAL2(x) (x) #define MACRO_EVAL(x) MACRO_EVAL2(x) @@ -51,4 +54,10 @@ #define SIZE_ARR(a) ( ((a)!=NULL) ? sizeof(a) / sizeof((a)[0]) : 0) +// max nodes +#define MAXSEG MaxSeg +#define LOG_MAXSEG LogMaxSeg +#define MAXSEG_REMAINDER_BITMASK MaxSegRemainderBitMask + + #endif // __dynarr_common_h__ \ No newline at end of file diff --git a/dynarrc/dynarr.c b/dynarrc/dynarr.c index 06ac2cb..7dc5ae8 100644 --- a/dynarrc/dynarr.c +++ b/dynarrc/dynarr.c @@ -1,33 +1,11 @@ +/**/ #include "common.h" -#include -#include - -#define MAXSEG 5 -#define DYNARR_TYPE int -#define TYPED_NAME( name ) TOKEN_PASTE(DYNARR_TYPE, name) -#include "dynarr.h" -typedef struct TYPED_NAME(DynArr) IdArr; - - -#define DYNARR_TYPE float -#define TYPED_NAME( name ) TOKEN_PASTE(DYNARR_TYPE, name) -#include "dynarr.h" -typedef struct TYPED_NAME(DynArr) ValueArr; - - -int main(int argc, char** argv){ - MEM_LEAK_CATCH(); - - IdArr *p1; - ValueArr *p2; - - p1 = (IdArr*)malloc(sizeof(IdArr)); - p2 = (ValueArr*)malloc(sizeof(ValueArr)); - - - // done - FREE_SAFE(p1); - FREE_SAFE(p2); - MEM_LEAK_SHOW(); - return 0; -} \ No newline at end of file +#define EXTERN + +/* Some global variables used externally */ +/* Maximum number of segments allowed to be in the system. Must be a power of 2 */ +EXTERN unsigned int MaxSeg; +/* Base-2 log */ +EXTERN unsigned int LogMaxSeg; +/* MAXSEG-1; MAXSEG must be a power of 2 */ +EXTERN unsigned int MaxSegRemainderBitMask; diff --git a/dynarrc/dynarr.h b/dynarrc/dynarr.h index 7d214d3..47a0b94 100644 --- a/dynarrc/dynarr.h +++ b/dynarrc/dynarr.h @@ -16,8 +16,62 @@ #pragma message("DYNARR_TYPE must be defined before including " __FILE__) #else // everything is fine +#include "dynarr_freenodelist.h" +// declarations +// swap two integers, or pointers, without using temporary storage +__inline void int_swap(unsigned int *var1, unsigned int *var2) { + (*var1) = (*var1) ^ (*var2); + (*var2) = (*var1) ^ (*var2); + (*var1) = (*var1) ^ (*var2); +} +// external definitions for allocating/resizing blocks +extern int seg_bank_alloc_next(); +extern int seg_bank_increase_max(); + + + +#define SEG_ARR_NAME pSegArr +#define FREESEG_LIST_NAME pFreeSegList +#define FREESEG_INVALID_LIST_NAME pFreeSegInvalidList + +#define SEG_INSERT_POS iInsertPos + +#define EMPTY_INDEX 0 +#define EMPTY_BYTE 0 +#define NULL_SEG emptySeg +#define SEG_INSERT_POS_BEGIN 1 + +// Node blocks +#if _REGION_ +// maximum number of segment banks +// this should be a very big number +// this number should never be reached +#define MAX_SEG_BANKS INT_MAX + +// array containing size (# of segments) of each segment bank +#define SEG_BANK_SIZE_ARR pSegBankSizeArr + +// array containing last valid index of each segment bank +#define SEG_BANK_INDEX_LIMIT_ARR pSegBankIndexLimitArr + +// Number of active segment banks +#define NUM_SEG_BANKS NumSegBanks +#define CURRENT_SEG_BANK (NUM_SEG_BANKS - 1) +// actual segment banks +#define SEG_BANKS_ARR pSegBanksArr + +// coordinates in the 2-D segment allocation +#define BANK_INDEX_FROM_SEG_INDEX(index) \ + ( ((unsigned int)(index)) >> LOG_MAXSEG ) + +#define BANK_FROM_SEG_INDEX(index) SEG_BANKS_ARR[BANK_INDEX_FROM_SEG_INDEX(index)] +#define SLOT_FROM_SEG_INDEX(index) (((unsigned int)(index)) & MAXSEG_REMAINDER_BITMASK) + +// pointer to the segment +#define PTR_SEG_FROM_INDEX(index) \ + (BANK_FROM_SEG_INDEX(index) + NUM_SEG_MEMBERS_IN_ARR * SLOT_FROM_SEG_INDEX(index) ) +#endif -// prefixing /* The dynamic array data structure */ struct TYPED_NAME(DynArr){ diff --git a/dynarrc/dynarr_freenodelist.h b/dynarrc/dynarr_freenodelist.h new file mode 100644 index 0000000..dfeabd4 --- /dev/null +++ b/dynarrc/dynarr_freenodelist.h @@ -0,0 +1,11 @@ +#ifndef __dynarr_free_node_list_h__ +#define __dynarr_free_node_list_h__ + +// the struct FreeSegNode will be used multiple times, so we do not want to redefine it +struct FreeSegNode{ + int index; + struct FreeSegNode* next; +}; +typedef struct FreeSegNode FreeSegNode; + +#endif \ No newline at end of file diff --git a/dynarrc/main.c b/dynarrc/main.c new file mode 100644 index 0000000..06ac2cb --- /dev/null +++ b/dynarrc/main.c @@ -0,0 +1,33 @@ +#include "common.h" +#include +#include + +#define MAXSEG 5 +#define DYNARR_TYPE int +#define TYPED_NAME( name ) TOKEN_PASTE(DYNARR_TYPE, name) +#include "dynarr.h" +typedef struct TYPED_NAME(DynArr) IdArr; + + +#define DYNARR_TYPE float +#define TYPED_NAME( name ) TOKEN_PASTE(DYNARR_TYPE, name) +#include "dynarr.h" +typedef struct TYPED_NAME(DynArr) ValueArr; + + +int main(int argc, char** argv){ + MEM_LEAK_CATCH(); + + IdArr *p1; + ValueArr *p2; + + p1 = (IdArr*)malloc(sizeof(IdArr)); + p2 = (ValueArr*)malloc(sizeof(ValueArr)); + + + // done + FREE_SAFE(p1); + FREE_SAFE(p2); + MEM_LEAK_SHOW(); + return 0; +} \ No newline at end of file