Skip to content
Permalink
Browse files

In the middle of a change. Committing for safety.

Signed-off-by: saad0105050 <saad0105050@gmail.com>
  • Loading branch information...
saad0105050
saad0105050 committed Oct 1, 2014
1 parent ec27b6b commit 3471efde506149ad699490edaf37d82106b89019
Showing with 118 additions and 33 deletions.
  1. +9 −0 dynarrc/common.h
  2. +10 −32 dynarrc/dynarr.c
  3. +55 −1 dynarrc/dynarr.h
  4. +11 −0 dynarrc/dynarr_freenodelist.h
  5. +33 −0 dynarrc/main.c
@@ -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__
@@ -1,33 +1,11 @@
/**/
#include "common.h"
#include <stdio.h>
#include <stdlib.h>

#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;
}
#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;
@@ -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){
@@ -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
@@ -0,0 +1,33 @@
#include "common.h"
#include <stdio.h>
#include <stdlib.h>

#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;
}

0 comments on commit 3471efd

Please sign in to comment.
You can’t perform that action at this time.