29 #define DISABLE_DEBUGLOG 32 #include "simpleptrlist_p.h" 33 #include <gwenhywfar/debug.h> 55 static INTERNAL_PTRLIST *
_reallocPtrList(INTERNAL_PTRLIST *oldEntries, uint64_t totalEntries);
56 static INTERNAL_PTRLIST *
_copyPtrList(
const INTERNAL_PTRLIST *oldEntries, uint64_t totalEntries);
80 pl->maxEntries=startEntries;
96 pl->entryList=oldList->entryList;
99 pl->maxEntries=oldList->maxEntries;
100 pl->steps=oldList->steps;
101 pl->usedEntries=oldList->usedEntries;
102 pl->attachObjectFn=oldList->attachObjectFn;
103 pl->freeObjectFn=oldList->freeObjectFn;
104 pl->userIntData=oldList->userIntData;
105 pl->flags=oldList->flags | GWEN_SIMPLEPTRLIST_FLAGS_COPYONWRITE;
107 oldList->flags|=GWEN_SIMPLEPTRLIST_FLAGS_COPYONWRITE;
116 assert(pl->refCount);
125 assert(pl->refCount);
126 if (pl->refCount==1) {
151 assert(pl->refCount);
156 ptr=pl->entryList->entries;
157 for (i=0; i<pl->usedEntries; i++)
166 assert(pl->refCount);
167 return pl->userIntData;
175 assert(pl->refCount);
184 assert(pl->refCount);
193 assert(pl->refCount);
194 return pl->userCounter;
202 assert(pl->refCount);
211 assert(pl->refCount);
212 if (pl->userCounter) {
228 assert(pl->refCount);
229 if (idx<pl->usedEntries) {
230 return pl->entryList->entries[idx];
234 "Index outside boundaries (%lu >= %lu)",
236 (
unsigned long)(pl->usedEntries));
246 assert(pl->refCount);
248 if (idx<pl->usedEntries) {
259 oldPtr=pl->entryList->entries[idx];
260 pl->entryList->entries[idx]=p;
281 assert(pl->refCount);
289 if (pl->usedEntries >= pl->maxEntries) {
292 num=pl->maxEntries+pl->steps;
293 if (num>pl->maxEntries) {
294 INTERNAL_PTRLIST *entryList;
298 if (entryList==
NULL) {
302 pl->entryList=entryList;
312 pl->entryList->entries[pl->usedEntries]=p;
316 return pl->usedEntries-1;
324 assert(pl->refCount);
333 assert(pl->refCount);
342 assert(pl->refCount);
343 return pl->maxEntries;
351 assert(pl->refCount);
352 return pl->usedEntries;
360 assert(pl->refCount);
361 return pl->entryList->entries;
369 assert(pl->refCount);
378 assert(pl->refCount);
387 assert(pl->refCount);
396 assert(pl->refCount);
408 assert(pl->refCount);
410 oldFn=pl->attachObjectFn;
411 pl->attachObjectFn=fn;
423 assert(pl->refCount);
425 oldFn=pl->freeObjectFn;
434 if (pl->attachObjectFn)
435 pl->attachObjectFn(pl, p);
442 if (pl->freeObjectFn)
443 pl->freeObjectFn(pl, p);
450 if (pl->attachObjectFn) {
455 ptr=pl->entryList->entries;
456 for (i=0; i<pl->usedEntries; i++) {
471 if (pl->freeObjectFn) {
476 ptr=pl->entryList->entries;
477 for (i=0; i<pl->usedEntries; i++) {
495 INTERNAL_PTRLIST *entries;
499 objectSize=
sizeof(INTERNAL_PTRLIST) + (totalEntries*
sizeof(
void *));
500 entries=(INTERNAL_PTRLIST *) malloc(objectSize);
505 memset((
void *)entries, 0, objectSize);
506 entries->refCounter=1;
507 entries->storedEntries=totalEntries;
515 assert(entries && entries->refCounter>0);
516 if (entries && entries->refCounter>0) {
518 entries->refCounter++;
529 if (entries && entries->refCounter>0) {
530 if (entries->refCounter==1) {
532 entries->refCounter=0;
536 entries->refCounter--;
545 assert(entries && entries->refCounter>0);
546 if (entries && entries->refCounter>0) {
548 uint64_t diffEntries;
551 if (totalEntries<entries->storedEntries) {
556 diffEntries=totalEntries-(entries->storedEntries);
557 newSize=
sizeof(INTERNAL_PTRLIST)+totalEntries*
sizeof(
void *);
559 entries=(INTERNAL_PTRLIST *) realloc(entries, newSize);
567 memset((
void *) &(entries->entries[entries->storedEntries]), 0, diffEntries*
sizeof(
void *));
568 entries->storedEntries=totalEntries;
579 INTERNAL_PTRLIST *
_copyPtrList(
const INTERNAL_PTRLIST *oldEntries, uint64_t totalEntries)
581 assert(oldEntries && oldEntries->refCounter>0);
582 if (oldEntries && oldEntries->refCounter>0) {
583 INTERNAL_PTRLIST *entries;
586 uint64_t diffEntries;
589 if (totalEntries<oldEntries->storedEntries)
590 totalEntries=oldEntries->storedEntries;
592 diffEntries=totalEntries-(oldEntries->storedEntries);
593 oldSize=
sizeof(INTERNAL_PTRLIST)+((oldEntries->storedEntries)*
sizeof(
void *));
594 newSize=
sizeof(INTERNAL_PTRLIST)+totalEntries*
sizeof(
void *);
596 entries=(INTERNAL_PTRLIST *) malloc(newSize);
603 memmove(entries, oldEntries, oldSize);
607 memset((
void *) &(entries->entries[entries->storedEntries]), 0, diffEntries*
sizeof(
void *));
610 entries->refCounter=1;
611 entries->storedEntries=totalEntries;
624 assert(pl && pl->refCount);
626 if (pl->flags & GWEN_SIMPLEPTRLIST_FLAGS_COPYONWRITE) {
627 INTERNAL_PTRLIST *entryList;
630 num=pl->maxEntries+pl->steps;
636 if (entryList==
NULL) {
642 pl->entryList=entryList;
650 pl->flags&=~GWEN_SIMPLEPTRLIST_FLAGS_COPYONWRITE;
uint64_t GWEN_SimplePtrList_GetMaxEntries(const GWEN_SIMPLEPTRLIST *pl)
void * GWEN_SimplePtrList_GetPtrAt(const GWEN_SIMPLEPTRLIST *pl, uint64_t idx)
int GWEN_SimplePtrList_DecUserCounter(GWEN_SIMPLEPTRLIST *pl)
#define GWEN_INHERIT_FINI(t, element)
#define GWEN_ERROR_INVALID
GWENHYWFAR_CB void(* GWEN_SIMPLEPTRLIST_ATTACHOBJECT_FN)(GWEN_SIMPLEPTRLIST *pl, void *p)
void GWEN_SimplePtrList_SetUserCounter(GWEN_SIMPLEPTRLIST *pl, uint64_t i)
void GWEN_SimplePtrList_IncUserCounter(GWEN_SIMPLEPTRLIST *pl)
GWENHYWFAR_CB void(* GWEN_SIMPLEPTRLIST_FREEOBJECT_FN)(GWEN_SIMPLEPTRLIST *pl, void *p)
uint32_t GWEN_SimplePtrList_GetFlags(const GWEN_SIMPLEPTRLIST *pl)
#define GWEN_FREE_OBJECT(varname)
#define DBG_VERBOUS(dbg_logger, format, args...)
static void _attachToAllObjects(GWEN_SIMPLEPTRLIST *pl)
static void _detachFromAllObjects(GWEN_SIMPLEPTRLIST *pl)
#define GWEN_ERROR_BUFFER_OVERFLOW
static INTERNAL_PTRLIST * _reallocPtrList(INTERNAL_PTRLIST *oldEntries, uint64_t totalEntries)
int GWEN_SimplePtrList_SetPtrAt(GWEN_SIMPLEPTRLIST *pl, uint64_t idx, void *p)
void GWEN_SimplePtrList_SetSteps(GWEN_SIMPLEPTRLIST *pl, uint64_t steps)
uint64_t GWEN_SimplePtrList_GetUserCounter(const GWEN_SIMPLEPTRLIST *pl)
#define GWEN_NEW_OBJECT(typ, varname)
static void _attachToObject(GWEN_SIMPLEPTRLIST *pl, void *p)
static INTERNAL_PTRLIST * _copyPtrList(const INTERNAL_PTRLIST *oldEntries, uint64_t totalEntries)
GWEN_SIMPLEPTRLIST * GWEN_SimplePtrList_LazyCopy(GWEN_SIMPLEPTRLIST *oldList)
void GWEN_SimplePtrList_Clear(GWEN_SIMPLEPTRLIST *pl)
void * GWEN_SimplePtrList_GetEntries(const GWEN_SIMPLEPTRLIST *pl)
#define GWEN_INHERIT_INIT(t, element)
struct GWEN_SIMPLEPTRLIST GWEN_SIMPLEPTRLIST
int GWEN_SimplePtrList_EnsureWritability(GWEN_SIMPLEPTRLIST *pl)
int GWEN_SimplePtrList_GetUserIntData(const GWEN_SIMPLEPTRLIST *pl)
#define GWEN_SIMPLEPTRLIST_FLAGS_ATTACHTOOBJECTS
static INTERNAL_PTRLIST * _mallocPtrList(uint64_t totalEntries)
static void _attachToPtrList(INTERNAL_PTRLIST *entries)
void GWEN_SimplePtrList_SubFlags(GWEN_SIMPLEPTRLIST *pl, uint32_t f)
void GWEN_SimplePtrList_Attach(GWEN_SIMPLEPTRLIST *pl)
#define DBG_ERROR(dbg_logger, format, args...)
static void _detachFromObject(GWEN_SIMPLEPTRLIST *pl, void *p)
static void _freePtrList(INTERNAL_PTRLIST *entries)
void GWEN_SimplePtrList_SetUserIntData(GWEN_SIMPLEPTRLIST *pl, int i)
void GWEN_SimplePtrList_AddFlags(GWEN_SIMPLEPTRLIST *pl, uint32_t f)
void GWEN_SimplePtrList_SetFlags(GWEN_SIMPLEPTRLIST *pl, uint32_t f)
GWEN_SIMPLEPTRLIST * GWEN_SimplePtrList_new(uint64_t startEntries, uint64_t steps)
GWEN_SIMPLEPTRLIST_FREEOBJECT_FN GWEN_SimplePtrList_SetFreeObjectFn(GWEN_SIMPLEPTRLIST *pl, GWEN_SIMPLEPTRLIST_FREEOBJECT_FN fn)
#define DBG_INFO(dbg_logger, format, args...)
uint64_t GWEN_SimplePtrList_GetUsedEntries(const GWEN_SIMPLEPTRLIST *pl)
GWEN_SIMPLEPTRLIST_ATTACHOBJECT_FN GWEN_SimplePtrList_SetAttachObjectFn(GWEN_SIMPLEPTRLIST *pl, GWEN_SIMPLEPTRLIST_ATTACHOBJECT_FN fn)
uint64_t GWEN_SimplePtrList_GetSteps(const GWEN_SIMPLEPTRLIST *pl)
int64_t GWEN_SimplePtrList_AddPtr(GWEN_SIMPLEPTRLIST *pl, void *p)
#define GWEN_SIMPLEPTRLIST_FLAGS_DETACHFROMOBJECTS
#define GWEN_INHERIT_FUNCTIONS(t)
#define GWEN_ERROR_MEMORY_FULL
void GWEN_SimplePtrList_free(GWEN_SIMPLEPTRLIST *pl)