30 #ifndef _UCOMMON_MEMORY_H_
31 #define _UCOMMON_MEMORY_H_
33 #ifndef _UCOMMON_CONFIG_H_
37 #ifndef _UCOMMON_PROTOCOLS_H_
41 #ifndef _UCOMMON_LINKED_H_
45 #ifndef _UCOMMON_STRING_H_
60 class __EXPORT
memalloc :
public MemoryProtocol
65 size_t pagesize, align;
68 typedef struct mempage {
90 virtual void fault(
void)
const;
118 inline unsigned max(
void)
const
125 inline unsigned size(
void)
const
138 unsigned utilization(
void)
const;
152 virtual void *_alloc(
size_t size);
178 pthread_mutex_t mutex;
187 virtual void _lock(
void);
192 virtual void _unlock(
void);
230 virtual void dealloc(
void *memory);
240 virtual void *
_alloc(
size_t size);
243 class __EXPORT ObjectPager :
protected memalloc
252 friend class ObjectPager;
254 inline void set(member *node)
257 inline void *
get(void)
const
260 member(LinkedObject **root);
264 inline void *operator*()
const
276 ObjectPager(
size_t objsize,
size_t pagesize = 256);
284 void *
get(
unsigned item)
const;
310 void *invalid(
void)
const;
324 inline ObjectPager::member *begin(
void)
325 {
return static_cast<ObjectPager::member *
>(root);};
327 inline operator bool()
const
328 {
return members > 0;}
330 inline bool operator!()
const
337 inline unsigned count(
void)
const
343 typedef linked_pointer<ObjectPager::member> iterator;
345 inline size_t size(
void)
348 inline unsigned pages(
void)
371 virtual const char *invalid(
void)
const;
381 virtual bool filter(
char *text,
size_t size);
397 inline void set(
member *node)
404 inline const char *operator*()
const
407 inline const char *
get(void)
const
432 const char *
get(
unsigned item)
const;
439 void set(
unsigned item,
const char *
string);
445 void add(
const char *text);
451 void push(
const char *text);
457 void push(
char **text);
463 const char *pull(
void);
469 const char *pop(
void);
476 void add(
char **list);
483 void set(
char **list);
500 inline const char *at(
unsigned item)
const
523 {add(text);
return *
this;}
526 {push(text);
return *
this;}
547 unsigned token(
const char *text,
const char *list,
const char *quote = NULL,
const char *end = NULL);
549 unsigned split(
const char *text,
const char *
string,
unsigned flags = 0);
551 unsigned split(stringex_t& expr,
const char *
string,
unsigned flags = 0);
553 String join(
const char *prefix = NULL,
const char *middle = NULL,
const char *suffix = NULL);
555 inline operator bool()
556 {
return members > 0;}
558 inline bool operator!()
561 inline StringPager& operator=(
char **list)
562 {set(list);
return *
this;}
564 inline const char *operator*()
567 inline operator char **()
575 inline size_t size(
void)
578 inline unsigned pages(
void)
605 virtual bool filter(
char *filename,
size_t size);
612 bool load(
const char *path);
619 void operator=(
const char *path);
621 inline const char *operator*()
const
624 inline operator bool()
const
625 {
return dir != NULL;};
627 inline bool operator!()
const
628 {
return dir == NULL;};
630 inline unsigned count(
void)
const
642 inline const char *
get(
unsigned item)
const
645 inline const char *at(
unsigned item)
const
648 inline size_t size(
void)
651 inline unsigned pages(
void)
662 typedef struct cpage {
668 cpage_t *first, *last, *current, *freelist;
670 unsigned long ccount;
674 virtual int _putch(
int code);
677 virtual void *
_alloc(
size_t size);
700 void set(
const char *text);
706 void add(
const char *text);
714 size_t get(
char *text,
size_t size);
721 void put(
const char *text,
size_t size);
727 inline unsigned long used(
void)
const
734 inline char *operator *()
742 {add(text);
return *
this;};
751 char *request(
size_t *iosize);
759 char *
copy(
size_t *iosize);
765 void update(
size_t size);
778 inline operator bool()
869 pthread_mutex_t mutex;
889 size_t inp, out, size;
896 charmem(
char *mem,
size_t size);
897 charmem(
size_t size);
903 void set(
char *mem,
size_t size);
905 void set(
size_t size);
907 inline void reset(
void)
910 inline void rewind(
void)
914 class __EXPORT chartext :
public CharacterProtocol
920 int _putch(
int code);
926 chartext(
char *buf,
size_t size);
953 keydata(
keyassoc *assoc,
const char *
id,
unsigned max,
unsigned bufsize);
956 friend class keydata;
971 void *allocate(
const char *name,
size_t size);
980 keyassoc(
unsigned indexing = 177,
size_t max = 0,
size_t page = 0);
1000 {
return locate(name);};
1012 void *locate(
const char *name);
1021 bool assign(
const char *name,
void *
pointer);
1029 bool create(
const char *name,
void *
pointer);
1037 void *
remove(
const char *name);
1040 template <
class T,
size_t P = 0>
1041 class listof :
private ObjectPager
1044 inline listof() : ObjectPager(sizeof(T), P) {};
1046 inline T& operator[](
unsigned item)
const
1047 {
return (T&)ObjectPager::get(item);}
1049 inline T* operator()(
unsigned item)
const
1050 {
return (T*)ObjectPager::get(item);}
1052 inline const T& at(
unsigned item)
const
1053 {
return (
const T&)ObjectPager::get(item);}
1055 inline T* pull(
void)
1056 {
return (T*)ObjectPager::pull();}
1059 {
return (T*)ObjectPager::pop();}
1061 inline operator T**()
1062 {
return (T**)ObjectPager::list();}
1064 inline T** list(
void)
1065 {
return (T**)ObjectPager::list();}
1067 inline T* operator++(
void)
1068 {T* tmp = ObjectPager::add();
if(tmp)
new((caddr_t)tmp) T;
return tmp;}
1070 inline T* add(
const T&
object)
1071 {T* tmp = ObjectPager::add();
if(tmp)
new((caddr_t)tmp) T(
object);
return tmp;}
1073 inline T* push(
const T&
object)
1074 {T* tmp = ObjectPager::push();
if(tmp)
new((caddr_t)tmp) T(
object);
return tmp;}
1076 inline listof& operator<<(
const T&
object)
1077 {T* tmp = ObjectPager::add();
if(tmp)
new((caddr_t)tmp) T(
object);
return *
this;}
1079 inline listof& operator>>(
const T&
object)
1080 {T* tmp = ObjectPager::push();
if(tmp)
new((caddr_t)tmp) T(
object);
return *
this;}
1084 template <
class T,
unsigned I = 177,
size_t M = 0,
size_t P = 0>
1085 class mapof :
private keyassoc
1091 inline mapof() : keyassoc(I, M, P) {};
1097 inline unsigned count(
void)
const
1098 {
return keyassoc::count();};
1103 inline void purge(
void)
1104 {keyassoc::purge();};
1111 inline T *locate(
const char *name)
1112 {
return static_cast<T*
>(keyassoc::locate(name));}
1114 inline T *operator[](
const char *name)
1115 {
return static_cast<T*
>(keyassoc::locate(name));}
1122 inline T *operator()(
const char *name)
1123 {
return locate(name);};
1129 inline T *map(
const char *name)
1130 {T *tmp = keyassoc::allocate(name,
sizeof(T));
if(tmp)
new((caddr_t)tmp) T;}
1137 inline void unmap(
const char *name)
1138 {keyassoc::remove(name);};
1145 inline unsigned utilization(
void)
1146 {
return mempager::utilization();};
1154 inline unsigned pages(
void)
const
1155 {
return mempager::pages();};
1165 template <
class T,
unsigned I = 177,
size_t M = 0,
size_t P = 0>
1179 {
return keyassoc::count();}
1185 {keyassoc::purge();}
1193 {
return static_cast<T*
>(keyassoc::locate(name));}
1195 inline T *operator[](
const char *name)
1196 {
return static_cast<T*
>(keyassoc::locate(name));}
1205 {
return locate(name);}
1215 {
return keyassoc::assign(name, pointer);}
1224 {
return keyassoc::create(name, pointer);}
1231 inline void remove(
char *name)
1232 {keyassoc::remove(name);}
1240 {
return mempager::utilization();}
1249 {
return mempager::pages();}
1258 template <
typename T>
1273 {
return new(
get(
sizeof(T))) T;};
1280 {
return new(
get(
sizeof(T))) T;};
1288 template <
class T,
unsigned M = 177>
1305 {NamedObject::purge(idx, M); mempager::purge();};
1313 inline T *
get(
const char *name)
const {
1314 T *node = (
static_cast<T*
>(NamedObject::map(idx, name, M)));
1316 node = init<T>(
static_cast<T*
>(mempager::_alloc(
sizeof(T))));
1317 node->NamedObject::add(idx, name, M);
1328 {
return NamedObject::map(idx, name, M) != NULL;};
1337 {
return get(name);};
1344 {
return static_cast<T*
>(NamedObject::skip(idx, NULL, M));};
1352 {
return static_cast<T*
>(NamedObject::skip(idx, current, M));};
1359 {
return NamedObject::count(idx, M);};
1368 {
return NamedObject::index(idx, M);};
1377 {
return NamedObject::sort(NamedObject::index(idx, M));};
1400 inline const char *shift(stringlist_t& list)
1401 {
return list.
pull();}
1403 inline void unshift(stringlist_t& list,
const char *text)
1407 inline String str(StringPager& list,
const char *prefix = NULL,
const char *middle = NULL,
const char *suffix = NULL)
1408 {
return list.join(prefix, middle, suffix);}
Pager pool base class for managed memory pools.
void purge(void)
Purge all allocated memory and heap pages immediately.
Mempager managed type factory for pager pool objects.
static unsigned count(const LinkedObject *root)
Count the number of linked objects in a list.
T * operator()(const char *name)
Reference a typed object directly by name.
T ** sort(void) const
Convert our hash map into an alphabetically sorted linear object pointer array.
unsigned count(void) const
Count the number of typed objects in our hash map.
keypager(size_t size)
Create the object cache.
StringPager & operator<<(const char *text)
Convenience operator to add to pager.
~keypager()
Destroy the hash pager by purging the index chains and memory pools.
A common string class and character string support functions.
virtual void dealloc(void)
Dealloc object no longer referenced.
Buffered pager for storing paged strings for character protocol.
unsigned count(void) const
Get the number of associations we have in our object.
A typed template for using a key association with typed objects.
bool test(const char *name) const
Test if a name exists in the pool.
Convenience class for directories.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
virtual void * _alloc(size_t size)
Allocate memory from the pager heap.
bufpager & operator<<(const char *text)
Convenience operator to add to pager.
const char * operator[](unsigned item) const
Return specified member from pager list.
unsigned max(void) const
Get the maximum number of pages that are permitted.
pager(mempager *heap=((void *) 0))
Construct a pager and optionally assign a private pager heap.
This is a base class for objects that may be created in pager pools.
void release(void)
Decrease reference count when released.
T * locate(const char *name)
Lookup a typed object by name.
bool create(char *name, T *pointer)
Create a new name in the association table and assign typed object.
void purge(void)
Purge all allocated memory and heap pages immediately.
StringPager stringlist_t
A convenience type for paged string lists.
unsigned pages(void) const
Get the number of pages that have been allocated from the real heap.
Common character processing protocol.
T * operator()(void)
Create a managed object by casting reference.
const char * pull(void)
Remove element from front of list.
A redirection base class for the memory protocol.
unsigned size(void) const
Get the size of a memory page.
StringPager::member * begin(void) const
Get root of pager list.
void * operator()(const char *name)
Lookup the data pointer of a string by direct operation.
void reset(void)
Force reset of count.
T * operator*()
Create a managed object by pointer reference.
void purge(void)
Purge the hash map of typed objects.
Abstract interfaces and support.
unsigned pages(void) const
Access to number of pages allocated from heap for our associated index pointer.
A linked object base class with members found by name.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
unsigned count(void) const
Get the number of items in the pager string list.
const char * get(unsigned item) const
Get string item from list.
T * operator[](const char *name) const
Find a typed object derived from NamedObject in the hash map by name.
A managed private heap for small allocations.
unsigned utilization(void) const
Determine fragmentation level of acquired heap pages.
Generic smart pointer class.
A smart pointer template for iterating linked lists.
bool assign(char *name, T *pointer)
Assign a name for a pointer to a typed object.
const char * operator[](unsigned item) const
Return specified filename from directory list.
virtual bool filter(char *text, size_t size)
Filter text in a derived class.
void operator+=(const char *text)
Convenience operator to add to pager and auto-sort.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
A memory protocol pager for private heap manager.
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
Common base class for all objects that can be formed into a linked list.
T ** index(void) const
Convert our hash map into a linear object pointer array.
unsigned count(void) const
Get the count of typed objects stored in our hash map.
assoc_pointer()
Construct an associated pointer hash map based on the class template.
Various miscellaneous platform specific headers and defines.
Directory pager is a paged string list for directory file names.
String pager for storing lists of NULL terminated strings.
bool operator!()
Check if can still save into buffer.
virtual int _putch(int code)=0
Put the next character.
A template class for a hash pager.
T * next(T *current) const
Find next typed object in hash map for iteration.
StringPager::member stringlistitem_t
A convenience type for paged string list items.
A base class for reference counted objects.
unsigned utilization(void) const
Access to pager utilization stats.
DirPager dirlist_t
A convenience type for using DirPager directly.
T * begin(void) const
Find first typed object in hash map to iterate.
Linked objects, lists, templates, and containers.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
A class to hold memory pointers referenced by string names.
Create a linked list of auto-releasable objects.
unsigned long used(void) const
Get total size.
virtual int _getch(void)=0
Get the next character.