Skip to content
Permalink
Browse files

incomplete.

Signed-off-by: unknown <saq10002@iteb-219.ad.engr.uconn.edu>
  • Loading branch information...
unknown
unknown committed Oct 1, 2014
1 parent 2f30e56 commit 5002e16e6eb0e3f26e3398560d90572432bc245f
Showing with 127 additions and 70 deletions.
  1. +2 −2 dynarrc/dynarr.c
  2. +125 −68 dynarrc/dynarr.h
@@ -24,9 +24,9 @@ SEGALLOC_USE();
int seg_bank_increase_max(){
void* pVoid;
int bSuccess = 1;
int prevMaxSegBanks = MaxSegBanks;
int prevMaxSegBanks = MAX_SEG_BANKS;
// new limit on number of segment banks
MaxSegBanks <<= 1;
MAX_SEG_BANKS <<= 1;

// realloc
if (pVoid = realloc(pSegBanksArr, MaxSegBanks * sizeof(real_t*))){
@@ -1,9 +1,9 @@
#pragma message("touching: "__FILE__)
#include "common.h"

//#define MAXSEG 5
//#define DYNARR_TYPE int
//#define TYPED_NAME(name) TOKEN_PASTE(DYNARR_TYPE, name)
#define MAXSEG 5
#define DYNARR_TYPE int
#define TYPED_NAME(name) TOKEN_PASTE(DYNARR_TYPE, name)
/* Assuming MAXSEG is defined.
Size of each block of the dynamic array.
Must be a positive integer.*/
@@ -20,6 +20,75 @@
#else
// everything is fine
#include "dynarr_freenodelist.h"



/* The segment structure. These are the nodes of the linked list */
typedef struct {
DYNARR_TYPE data;
int index; // my position
int nextIndex; // next node's position
}TYPED_NAME(Sseg);
const TYPED_NAME(Sseg) EMPTY_SSEG;

/* The dynamic array data structure */
typedef struct{
TYPED_NAME(Sseg)* pSegArr;
//real_t* pSegNextArr;
int iInsertPos;
/////////////////// Free nodes list ////////////////
struct FreeSegNode *pFreeSegList, *pFreeSegInvalidList;
int iSizeOfFreeSegList;
struct FreeSegNode* pTempFreeNode1, *pTempFreeNode2;
////////////// temporaries //////////////
int iTempIndex1, iTempIndex2;
TYPED_NAME(Sseg) tempSeg1;
const TYPED_NAME(Sseg) emptySeg;
//////////// Segment banks ////////////
TYPED_NAME(Sseg)** pSegBanksArr;
int* pSegBankSizeArr;
int* pSegBankIndexLimitArr;
int NumSegBanks;
int NumSegBanks;
int MaxSegBanks;
int SegBankAllocViolation;

}TYPED_NAME(DynArr);
// dynarr variable name; contains all state variables
#define DYNARR_VARS TOKEN_PASTE(TYPED_NAME(DynArr), Vars)

/* Shortcuts to important variables */
#define SEG_ARR DYNARR_VARS.pSegArr
#define SEG_INSERT_POS DYNARR_VARS.iInsertPos
#define FREESEG_LIST DYNARR_VARS.pFreeSegList
#define FREESEG_INVALID_LIST DYNARR_VARS.pFreeSegInvalidList
#define FREESEG_LIST_SIZE DYNARR_VARS.iSizeOfFreeSegList
#define TEMP_FREESEGNODE_1 DYNARR_VARS.pTempFreeNode1
#define TEMP_FREESEGNODE_2 DYNARR_VARS.pTempFreeNode2
#define TEMP_INDEX_1 DYNARR_VARS.iTempIndex1
#define TEMP_INDEX_2 DYNARR_VARS.iTempIndex2
#define TEMP_SEG_1 DYNARR_VARS.iTempIndex1
#define NULL_SEG DYNARR_VARS.emptySeg
#define SEG_BANK_ALLOC_VIOLATION DYNARR_VARS.SegBankAllocViolation
// maximum number of segment banks
// this should be a very big number
// this number should never be reached
#define MAX_SEG_BANKS DYNARR_VARS.MaxSegBanks

// array containing size (# of segments) of each segment bank
#define SEG_BANK_SIZE_ARR DYNARR_VARS.pSegBankSizeArr

// array containing last valid index of each segment bank
#define SEG_BANK_INDEX_LIMIT_ARR DYNARR_VARS.pSegBankIndexLimitArr

// Number of active segment banks
#define NUM_SEG_BANKS DYNARR_VARS.NumSegBanks
#define CURRENT_SEG_BANK (NUM_SEG_BANKS - 1)
// actual segment banks
#define SEG_BANKS_ARR DYNARR_VARS.pSegBanksArr



// declarations
// swap two integers, or pointers, without using temporary storage
__inline void int_swap(unsigned int *var1, unsigned int *var2) {
@@ -33,33 +102,15 @@ extern int seg_bank_increase_max();



#define SEG_ARR_NAME pSegArr

#define SEG_INSERT_POS iInsertPos

#define EMPTY_INDEX 0
#define EMPTY_BYTE 0
#define NULL_SEG emptySeg
#define NULL_SEG TYPED_NAME(DynArr).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) \
@@ -94,22 +145,12 @@ extern int seg_bank_increase_max();
#ifdef _REGION_


#define SEG_BANKS_USE() \
extern int NumSegBanks; \
extern int MaxSegBanks; \
extern real_t** SEG_BANKS_ARR; \
extern int* SEG_BANK_SIZE_ARR; \
extern int* SEG_BANK_INDEX_LIMIT_ARR; \
extern int SegBankAllocViolation; \

#define SEG_BANKS_CREATE() \
/* NumSegBanks must be initialized at once, because it may be needed by GPU */ \
int NumSegBanks = 1; /* Default value */ \
int MaxSegBanks = 1; /* Default value */ \
real_t** SEG_BANKS_ARR; \
int* SEG_BANK_SIZE_ARR; \
int* SEG_BANK_INDEX_LIMIT_ARR; \
int SegBankAllocViolation = 0; \
NUM_SEG_BANKS = 1; /* Default value */ \
MAX_SEG_BANKS = 1; /* Default value */ \
SEG_BANK_ALLOC_VIOLATION = 0; \


/* SEG_BANKS_INIT() must be called after/at the end of SEG_ARR_INIT() */
@@ -143,38 +184,19 @@ extern int seg_bank_increase_max();
/* DynArr methods*/

#define SEG_ARR_USE() \
extern real_t* pSegArr; \
extern real_t* pSegCArr; \
extern real_t* pSegVArr; \
extern real_t* pSegNextArr; \
extern real_t *pFirstSegIndexForPipe, *pLastSegIndexForPipe; \
extern int iInsertPos; \
extern struct FreeSegNode *pFreeSegList, *pFreeSegInvalidList; \
extern int iSizeOfFreeSegList; \
extern struct FreeSegNode* pTempFreeNode1, *pTempFreeNode2; \
extern int iTempIndex1, iTempIndex2; \
extern struct Sseg tempSeg1;\
extern const struct Sseg emptySeg;\
/* multiple segment banks */ \
SEG_BANKS_USE(); \
extern TYPED_NAME(DynArr) DYNARR_VARS;



#define SEG_ARR_CREATE() \
real_t* pSegArr = NULL; \
real_t* pSegCArr = NULL; \
real_t* pSegVArr = NULL; \
real_t* pSegNextArr = NULL; \
real_t *pFirstSegIndexForPipe = NULL; \
real_t *pLastSegIndexForPipe = NULL; \
int iInsertPos = SEG_INSERT_POS_BEGIN; \
struct FreeSegNode *pFreeSegList = NULL, *pFreeSegInvalidList = NULL; \
int iSizeOfFreeSegList = 0; \
struct FreeSegNode* pTempFreeNode1 = NULL, *pTempFreeNode2 = NULL; \
int iTempIndex1 = 0, iTempIndex2 = 0;\
const struct Sseg emptySeg = EMPTY_SSEG;\
struct Sseg tempSeg1; \
/* multiple segment banks */ \
TYPED_NAME(DynArr) DYNARR_VARS;\
SEG_ARR = NULL; \
SEG_INSERT_POS = SEG_INSERT_POS_BEGIN; \
FREESEG_LIST = NULL, FREESEG_INVALID_LIST = NULL; \
FREESEG_LIST_SIZE = 0; \
TEMP_FREESEGNODE_1 = NULL, TEMP_FREESEGNODE_2 = NULL; \
TEMP_INDEX_1 = 0, TEMP_INDEX_2 = 0;\
/* initialize struct types later */\
SEG_BANKS_CREATE(); \


@@ -227,11 +249,46 @@ extern int seg_bank_increase_max();
#define SEG_ARR_EXISTS() ( pSegArr != NULL )
#define SEG_ARR_SIZE NUM_SEG_ARR_ITEMS


/* The dynamic array data structure */
struct TYPED_NAME(DynArr){
int a;
};
// allocation/free
#define SEG_EPANET_FREE_SET(value_seg) \
( _SEG_ARR_FREE_LIST_ADD((value_seg)), \
TOTALSEGS_DECR(), \
SEG_ARR_SEG_NEXT_INDEX(value_seg) = (real_t) EMPTY_INDEX, \
(value_seg).index = (value_seg).nextIndex = EMPTY_INDEX \
)

#define SEG_EPANET_FREE_GET(value_seg) (\
_SEG_ARR_FREE_LIST_GRAB_INDEX((value_seg)), \
(SEG_EPANET_NOT_NULL(value_seg) ? TOTALSEGS_INCR() : 0), \
(value_seg) \
)

// should return NULL_SEG if alloc fails
#define SEG_EPANET_ALLOC(value_seg) (\
(SegBankAllocViolation) \
? ((value_seg).index = (value_seg).nextIndex = EMPTY_INDEX, (value_seg) ) \
: ( \
(value_seg).index = iInsertPos, \
(value_seg).nextIndex = EMPTY_INDEX, \
TOTALSEGS_INCR(), \
(iInsertPos >= SEG_BANK_INDEX_LIMIT_ARR[NUM_SEG_BANKS-1]) \
? /* last index filled. allocate more banks. */ \
seg_bank_alloc_next() \
? /* ok */ (void) 0 \
: /* further alloc not possible */ (\
SegBankAllocViolation = 1, \
printf("\nFatal error: No more segment allocation possible." \
"\n\tSegment index: %d, Last possible index: %d " \
"NumSegBanks: %d, MaxSegBanks: %d " \
"\n\tFile: %s\n\tLine: %d\n", \
iInsertPos, SEG_BANK_INDEX_LIMIT_ARR[CURRENT_SEG_BANK], \
NUM_SEG_BANKS, MAX_SEG_BANKS, \
__FILE__, __LINE__) \
) \
: /* last index not filled */ iInsertPos++, \
(value_seg)\
) \
) \


#endif

0 comments on commit 5002e16

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