TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DynamicArray.cpp File Reference
#include "../CascLib.h"
#include "../CascCommon.h"
+ Include dependency graph for DynamicArray.cpp:

Macros

#define __CASCLIB_SELF__
 

Functions

static bool EnlargeArray (PDYNAMIC_ARRAY pArray, size_t NewItemCount)
 
int Array_Create_ (PDYNAMIC_ARRAY pArray, size_t ItemSize, size_t ItemCountMax)
 
void * Array_Insert (PDYNAMIC_ARRAY pArray, const void *NewItems, size_t NewItemCount)
 
void * Array_ItemAt (PDYNAMIC_ARRAY pArray, size_t ItemIndex)
 
size_t Array_IndexOf (PDYNAMIC_ARRAY pArray, const void *ArrayPtr)
 
void Array_Free (PDYNAMIC_ARRAY pArray)
 

Macro Definition Documentation

#define __CASCLIB_SELF__

Function Documentation

int Array_Create_ ( PDYNAMIC_ARRAY  pArray,
size_t  ItemSize,
size_t  ItemCountMax 
)
52 {
53  pArray->ItemArray = CASC_ALLOC(char, (ItemSize * ItemCountMax));
54  if(pArray->ItemArray == NULL)
56 
57  pArray->ItemCountMax = ItemCountMax;
58  pArray->ItemCount = 0;
59  pArray->ItemSize = ItemSize;
60  return ERROR_SUCCESS;
61 }
#define CASC_ALLOC(type, count)
Definition: CascCommon.h:302
size_t ItemSize
Definition: DynamicArray.h:22
#define ERROR_NOT_ENOUGH_MEMORY
Definition: CascPort.h:208
arena_t NULL
Definition: jemalloc_internal.h:624
char * ItemArray
Definition: DynamicArray.h:19
size_t ItemCountMax
Definition: DynamicArray.h:20
#define ERROR_SUCCESS
Definition: CascPort.h:204
size_t ItemCount
Definition: DynamicArray.h:21
void Array_Free ( PDYNAMIC_ARRAY  pArray)
96 {
97  if(pArray != NULL && pArray->ItemArray != NULL)
98  {
99  CASC_FREE(pArray->ItemArray);
100  }
101 }
arena_t NULL
Definition: jemalloc_internal.h:624
#define CASC_FREE(ptr)
Definition: CascCommon.h:303
char * ItemArray
Definition: DynamicArray.h:19

+ Here is the caller graph for this function:

size_t Array_IndexOf ( PDYNAMIC_ARRAY  pArray,
const void *  ArrayPtr 
)
88 {
89  char * ArrayItem = (char *)ArrayPtr;
90 
91  assert(pArray->ItemArray <= ArrayItem && ArrayItem <= pArray->ItemArray + (pArray->ItemCount * pArray->ItemSize));
92  return ((ArrayItem - pArray->ItemArray) / pArray->ItemSize);
93 }
size_t ItemSize
Definition: DynamicArray.h:22
char * ItemArray
Definition: DynamicArray.h:19
size_t ItemCount
Definition: DynamicArray.h:21

+ Here is the caller graph for this function:

void* Array_Insert ( PDYNAMIC_ARRAY  pArray,
const void *  NewItems,
size_t  NewItemCount 
)
64 {
65  char * NewItemPtr;
66 
67  // Try to enlarge the buffer, if needed
68  if(!EnlargeArray(pArray, pArray->ItemCount + NewItemCount))
69  return NULL;
70  NewItemPtr = pArray->ItemArray + (pArray->ItemCount * pArray->ItemSize);
71 
72  // Copy the old item(s), if any
73  if(NewItems != NULL)
74  memcpy(NewItemPtr, NewItems, (NewItemCount * pArray->ItemSize));
75 
76  // Increment the size of the array
77  pArray->ItemCount += NewItemCount;
78  return NewItemPtr;
79 }
size_t ItemSize
Definition: DynamicArray.h:22
static bool EnlargeArray(PDYNAMIC_ARRAY pArray, size_t NewItemCount)
Definition: DynamicArray.cpp:18
arena_t NULL
Definition: jemalloc_internal.h:624
char * ItemArray
Definition: DynamicArray.h:19
size_t ItemCount
Definition: DynamicArray.h:21

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void* Array_ItemAt ( PDYNAMIC_ARRAY  pArray,
size_t  ItemIndex 
)
82 {
83  assert(ItemIndex < pArray->ItemCount);
84  return pArray->ItemArray + (ItemIndex * pArray->ItemSize);
85 }
size_t ItemSize
Definition: DynamicArray.h:22
char * ItemArray
Definition: DynamicArray.h:19

+ Here is the caller graph for this function:

static bool EnlargeArray ( PDYNAMIC_ARRAY  pArray,
size_t  NewItemCount 
)
static
19 {
20  char * NewItemArray;
21  size_t ItemCountMax;
22 
23  // We expect the array to be already allocated
24  assert(pArray->ItemArray != NULL);
25  assert(pArray->ItemCountMax != 0);
26 
27  // Shall we enlarge the table?
28  if(NewItemCount > pArray->ItemCountMax)
29  {
30  // Calculate new table size
31  ItemCountMax = pArray->ItemCountMax;
32  while(ItemCountMax < NewItemCount)
33  ItemCountMax = ItemCountMax << 1;
34 
35  // Allocate new table
36  NewItemArray = CASC_REALLOC(char, pArray->ItemArray, pArray->ItemSize * ItemCountMax);
37  if(NewItemArray == NULL)
38  return false;
39 
40  // Set the new table size
41  pArray->ItemCountMax = ItemCountMax;
42  pArray->ItemArray = NewItemArray;
43  }
44 
45  return true;
46 }
size_t ItemSize
Definition: DynamicArray.h:22
arena_t NULL
Definition: jemalloc_internal.h:624
#define CASC_REALLOC(type, ptr, count)
Definition: CascCommon.h:301
char * ItemArray
Definition: DynamicArray.h:19
size_t ItemCountMax
Definition: DynamicArray.h:20

+ Here is the caller graph for this function: