31 #ifndef _UCOMMON_MEMORY_H_
32 #define _UCOMMON_MEMORY_H_
34 #ifndef _UCOMMON_CONFIG_H_
38 #ifndef _UCOMMON_PROTOCOLS_H_
42 #ifndef _UCOMMON_LINKED_H_
46 #ifndef _UCOMMON_STRING_H_
61 class __EXPORT
memalloc :
public MemoryProtocol
69 size_t pagesize, align;
72 typedef struct mempage {
94 virtual void fault(
void)
const;
122 inline unsigned max(
void)
const
129 inline unsigned size(
void)
const
130 {
return (
unsigned)pagesize;}
142 unsigned utilization(
void)
const;
157 virtual void *_alloc(
size_t size);
195 mutable pthread_mutex_t mutex;
206 virtual void _lock(
void);
211 virtual void _unlock(
void);
235 unsigned utilization(
void);
249 virtual void dealloc(
void *memory);
260 virtual void *_alloc(
size_t size);
275 class __EXPORT ObjectPager :
protected memalloc
284 friend class ObjectPager;
286 inline void set(member *node)
289 inline void *
get(void)
const
292 member(LinkedObject **root);
296 inline void *operator*()
const
307 inline ObjectPager(
const ObjectPager& ref) {};
310 ObjectPager(
size_t objsize,
size_t pagesize = 256);
318 void *
get(
unsigned item)
const;
344 void *invalid(
void)
const;
358 inline ObjectPager::member *begin(
void)
359 {
return static_cast<ObjectPager::member *
>(root);}
361 inline operator bool()
const
362 {
return members > 0;}
364 inline bool operator!()
const
371 inline unsigned count(
void)
const
377 typedef linked_pointer<ObjectPager::member> iterator;
379 inline size_t size(
void)
382 inline unsigned pages(
void)
397 void assign(ObjectPager& source);
399 inline ObjectPager& operator=(ObjectPager& source) {
419 virtual const char *invalid(
void)
const;
429 virtual bool filter(
char *text,
size_t size);
445 inline void set(
member *node)
452 inline const char *operator*()
const
455 inline const char *
get(void)
const
480 const char *
get(
unsigned item)
const;
487 void set(
unsigned item,
const char *
string);
493 void add(
const char *text);
499 void push(
const char *text);
505 void push(
char **text);
511 const char *pull(
void);
517 const char *pop(
void);
524 void add(
char **list);
531 void set(
char **list);
548 inline const char *at(
unsigned item)
const
571 {add(text);
return *
this;}
574 {push(text);
return *
this;}
595 unsigned token(
const char *text,
const char *list,
const char *quote = NULL,
const char *end = NULL);
597 unsigned split(
const char *text,
const char *
string,
unsigned flags = 0);
599 unsigned split(stringex_t& expr,
const char *
string,
unsigned flags = 0);
601 String join(
const char *prefix = NULL,
const char *middle = NULL,
const char *suffix = NULL);
603 inline operator bool()
604 {
return members > 0;}
606 inline bool operator!()
609 inline StringPager& operator=(
char **list)
610 {set(list);
return *
this;}
612 inline const char *operator*()
615 inline operator char **()
623 inline size_t size(
void)
const
626 inline unsigned pages(
void)
const
638 void assign(StringPager& source);
640 inline StringPager& operator=(StringPager& source) {
668 virtual bool filter(
char *filename,
size_t size);
675 bool load(
const char *path);
682 void operator=(
const char *path);
684 inline const char *operator*()
const
687 inline operator bool()
const
688 {
return dir != NULL;}
690 inline bool operator!()
const
691 {
return dir == NULL;}
693 inline unsigned count(
void)
const
705 inline const char *
get(
unsigned item)
const
708 inline const char *at(
unsigned item)
const
711 inline size_t size(
void)
const
714 inline unsigned pages(
void)
const
722 void assign(DirPager& source);
724 inline DirPager& operator=(DirPager& source) {
737 typedef struct cpage {
743 cpage_t *first, *last, *current, *freelist;
745 unsigned long ccount;
748 virtual int _getch(
void);
749 virtual int _putch(
int code);
752 virtual void *_alloc(
size_t size);
775 void set(
const char *text);
781 void add(
const char *text);
789 size_t get(
char *text,
size_t size);
796 void put(
const char *text,
size_t size);
802 inline unsigned long used(
void)
const
809 inline char *operator *()
817 {add(text);
return *
this;}
826 char *request(
size_t *iosize);
834 char *
copy(
size_t *iosize);
840 void update(
size_t size);
853 inline operator bool()
const
955 mutable pthread_mutex_t mutex;
975 size_t inp, out, size;
979 int _putch(
int code);
982 charmem(
char *mem,
size_t size);
983 charmem(
size_t size);
989 void set(
char *mem,
size_t size);
991 void set(
size_t size);
993 inline void reset(
void)
996 inline void rewind(
void)
1000 class __EXPORT chartext :
public CharacterProtocol
1006 int _putch(
int code);
1011 chartext(
char *buf);
1012 chartext(
char *buf,
size_t size);
1013 virtual ~chartext();
1039 keydata(
keyassoc *assoc,
const char *
id,
unsigned max,
unsigned bufsize);
1042 friend class keydata;
1057 void *allocate(
const char *name,
size_t size);
1066 keyassoc(
unsigned indexing = 177,
size_t max = 0,
size_t page = 0);
1086 {
return locate(name);}
1098 void *locate(
const char *name);
1107 bool assign(
const char *name,
void *
pointer);
1115 bool create(
const char *name,
void *
pointer);
1123 void *
remove(
const char *name);
1126 template <
class T,
size_t P = 0>
1127 class listof :
private ObjectPager
1130 inline listof() : ObjectPager(sizeof(T), P) {}
1132 inline T& operator[](
unsigned item)
const
1133 {
return (T&)ObjectPager::get(item);}
1135 inline T* operator()(
unsigned item)
const
1136 {
return (T*)ObjectPager::get(item);}
1138 inline const T& at(
unsigned item)
const
1139 {
return (
const T&)ObjectPager::get(item);}
1141 inline T* pull(
void)
1142 {
return (T*)ObjectPager::pull();}
1145 {
return (T*)ObjectPager::pop();}
1147 inline operator T**()
1148 {
return (T**)ObjectPager::list();}
1150 inline T** list(
void)
1151 {
return (T**)ObjectPager::list();}
1153 inline T* operator++(
void)
1154 {T* tmp = ObjectPager::add();
if(tmp)
new((caddr_t)tmp) T;
return tmp;}
1156 inline T* add(
const T&
object)
1157 {T* tmp = ObjectPager::add();
if(tmp)
new((caddr_t)tmp) T(
object);
return tmp;}
1159 inline T* push(
const T&
object)
1160 {T* tmp = ObjectPager::push();
if(tmp)
new((caddr_t)tmp) T(
object);
return tmp;}
1162 inline listof& operator<<(
const T&
object)
1163 {T* tmp = ObjectPager::add();
if(tmp)
new((caddr_t)tmp) T(
object);
return *
this;}
1165 inline listof& operator>>(
const T&
object)
1166 {T* tmp = ObjectPager::push();
if(tmp)
new((caddr_t)tmp) T(
object);
return *
this;}
1170 template <
class T,
unsigned I = 177,
size_t M = 0,
size_t P = 0>
1171 class mapof :
private keyassoc
1177 inline mapof() :
keyassoc(I, M, P) {}
1183 inline unsigned count(
void)
const
1189 inline void purge(
void)
1197 inline T *locate(
const char *name)
1200 inline T *operator[](
const char *name)
1208 inline T *operator()(
const char *name)
1209 {
return locate(name);}
1215 inline T *map(
const char *name)
1223 inline void unmap(
const char *name)
1240 inline unsigned pages(
void)
const
1251 template <
class T,
unsigned I = 177,
size_t M = 0,
size_t P = 0>
1281 inline T *operator[](
const char *name)
1317 inline void remove(
char *name)
1344 template <
typename T>
1359 {
return new(
get(
sizeof(T))) T;}
1366 {
return new(
get(
sizeof(T))) T;}
1374 template <
class T,
unsigned M = 177>
1399 inline T *
get(
const char *name)
const {
1403 node->NamedObject::add(idx, name, M);
1486 inline const char *shift(stringlist_t& list)
1487 {
return list.
pull();}
1489 inline void unshift(stringlist_t& list,
const char *text)
1493 inline String str(StringPager& list,
const char *prefix = NULL,
const char *middle = NULL,
const char *suffix = NULL)
1494 {
return list.join(prefix, middle, suffix);}
unsigned utilization(void) const
Access to pager utilization stats.
A common string class and character string support functions.
T * next(T *current) const
Find next typed object in hash map for iteration.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
static void purge(NamedObject **hash, unsigned size)
Purge a hash indexed table of named objects.
const char * operator[](unsigned item) const
Return specified member from pager list.
const char * operator[](unsigned item) const
Return specified filename from directory list.
void * remove(const char *name)
Remove a name and pointer association.
StringPager stringlist_t
A convenience type for paged string lists.
A base class for reference counted objects.
assoc_pointer()
Construct an associated pointer hash map based on the class template.
unsigned long used(void) const
Get total size.
Various miscellaneous platform specific headers and defines.
bool assign(char *name, T *pointer)
Assign a name for a pointer to a typed object.
bool create(const char *name, void *pointer)
Create a new name in the association table and assign it's value.
StringPager::member stringlistitem_t
A convenience type for paged string list items.
bufpager & operator<<(const char *text)
Convenience operator to add to pager.
unsigned pages(void) const
Get the number of pages that have been allocated from the real heap.
bool create(char *name, T *pointer)
Create a new name in the association table and assign typed object.
T * locate(const char *name)
Lookup a typed object by name.
A managed private heap for small allocations.
A class to hold memory pointers referenced by string names.
Convenience class for directories.
unsigned count(void) const
Count the number of typed objects in our hash map.
Common base class for all objects that can be formed into a linked list.
void purge(void)
Purge all associations and return allocated pages to heap.
const char * pull(void)
Remove element from front of list.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
unsigned pages(void) const
Access to number of pages allocated from heap for our associated index pointer.
T * begin(void) const
Find first typed object in hash map to iterate.
Buffered pager for storing paged strings for character protocol.
Linked objects, lists, templates, and containers.
A typed template for using a key association with typed objects.
This is a base class for objects that may be created in pager pools.
static NamedObject * map(NamedObject **hash, const char *name, unsigned size)
Find a named object through a hash map table.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
A redirection base class for the memory protocol.
unsigned size(void) const
Get the size of a memory page.
void * locate(const char *name)
Lookup the data pointer by the string name given.
unsigned utilization(void)
Determine fragmentation level of acquired heap pages.
virtual void * _alloc(size_t size)
Allocate memory from the pager heap.
T ** sort(void) const
Convert our hash map into an alphabetically sorted linear object pointer array.
Create a linked list of auto-releasable objects.
unsigned count(void) const
Get the number of items in the pager string list.
Pager pool base class for managed memory pools.
bool operator!() const
Check if can still save into buffer.
bool assign(const char *name, void *pointer)
Assign a name to a data pointer.
unsigned max(void) const
Get the maximum number of pages that are permitted.
Generic smart pointer class.
~keypager()
Destroy the hash pager by purging the index chains and memory pools.
bool test(const char *name) const
Test if a name exists in the pool.
void purge(void)
Purge all allocated memory and heap pages immediately.
pager(mempager *heap=((void *) 0))
Construct a pager and optionally assign a private pager heap.
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
T * operator()(const char *name)
Reference a typed object directly by name.
Common namespace for all ucommon objects.
keypager(size_t size)
Create the object cache.
static unsigned count(NamedObject **hash, unsigned size)
Count the total named objects in a hash table.
Mempager managed type factory for pager pool objects.
static NamedObject ** index(NamedObject **hash, unsigned size)
Convert a hash index into a linear object pointer array.
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.
A smart pointer template for iterating linked lists.
static NamedObject ** sort(NamedObject **list, size_t count=0)
Sort an array of named objects in alphabetical order.
T * operator()(void)
Create a managed object by casting reference.
A memory protocol pager for private heap manager.
String pager for storing lists of NULL terminated strings.
static NamedObject * skip(NamedObject **hash, NamedObject *current, unsigned size)
Iterate through a hash map table.
void * operator()(const char *name)
Lookup the data pointer of a string by direct operation.
void * allocate(const char *name, size_t size)
Allocate object stored in pager also.
StringPager::member * begin(void) const
Get root of pager list.
const char * get(unsigned item) const
Get string item from list.
linked_pointer< T > iterator
Convenience typedef for iterative pointer.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
A template class for a hash pager.
keyassoc(unsigned indexing=177, size_t max=0, size_t page=0)
Create a key associated memory pointer table.
Abstract interfaces and support.
T * operator[](const char *name) const
Find a typed object derived from NamedObject in the hash map by name.
unsigned count(void) const
Get the number of associations we have in our object.
void purge(void)
Purge the hash map of typed objects.
A linked object base class with members found by name.
DirPager dirlist_t
A convenience type for using DirPager directly.
linked_pointer< StringPager::member > iterator
Convenience typedef for iterative pointer.
void operator+=(const char *text)
Convenience operator to add to pager and auto-sort.
StringPager & operator<<(const char *text)
Convenience operator to add to pager.
Common character processing protocol.
T * operator*()
Create a managed object by pointer reference.
Directory pager is a paged string list for directory file names.