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 mutable pthread_mutex_t mutex;
187 virtual void _lock(
void);
192 virtual void _unlock(
void);
216 unsigned utilization(
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)
const
578 inline unsigned pages(
void)
const
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)
const
651 inline unsigned pages(
void)
const
662 typedef struct cpage {
668 cpage_t *first, *last, *current, *freelist;
670 unsigned long ccount;
673 virtual int _getch(
void);
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()
const
869 mutable pthread_mutex_t mutex;
889 size_t inp, out, size;
893 int _putch(
int code);
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
1103 inline void purge(
void)
1111 inline T *locate(
const char *name)
1114 inline T *operator[](
const char *name)
1122 inline T *operator()(
const char *name)
1123 {
return locate(name);}
1129 inline T *map(
const char *name)
1137 inline void unmap(
const char *name)
1154 inline unsigned pages(
void)
const
1165 template <
class T,
unsigned I = 177,
size_t M = 0,
size_t P = 0>
1195 inline T *operator[](
const char *name)
1231 inline void remove(
char *name)
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>
1313 inline T *
get(
const char *name)
const {
1317 node->NamedObject::add(idx, name, 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);}
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
unsigned utilization(void) const
Access to pager utilization stats.
T * next(T *current) const
Find next typed object in hash map for iteration.
const char * operator[](unsigned item) const
Return specified filename from directory list.
void * remove(const char *name)
Remove a name and pointer association.
static unsigned count(NamedObject **hash, unsigned size)
Count the total named objects in a hash table.
A base class for reference counted objects.
void operator+=(const char *text)
Convenience operator to add to pager and auto-sort.
assoc_pointer()
Construct an associated pointer hash map based on the class template.
unsigned long used(void) const
Get total size.
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.
bufpager & operator<<(const char *text)
Convenience operator to add to pager.
StringPager::member stringlistitem_t
A convenience type for paged string list items.
Pager pool base class for managed memory pools.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
static NamedObject ** sort(NamedObject **list, size_t count=0)
Sort an array of named objects in alphabetical order.
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 redirection base class for the memory protocol.
StringPager stringlist_t
A convenience type for paged string lists.
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 namespace for all ucommon objects.
void purge(void)
Purge all associations and return allocated pages to heap.
Linked objects, lists, templates, and containers.
static void purge(NamedObject **hash, unsigned size)
Purge a hash indexed table of named objects.
unsigned pages(void) const
Get the number of pages that have been allocated from the real heap.
static NamedObject ** index(NamedObject **hash, unsigned size)
Convert a hash index into a linear object pointer array.
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.
Generic smart pointer class.
A typed template for using a key association with typed objects.
Mempager managed type factory for pager pool objects.
void purge(void)
Purge all allocated memory and heap pages immediately.
linked_pointer< StringPager::member > iterator
Convenience typedef for iterative pointer.
A memory protocol pager for private heap manager.
virtual void * _alloc(size_t size)
Allocate memory from the pager heap.
Common base class for all objects that can be formed into a linked list.
void * locate(const char *name)
Lookup the data pointer by the string name given.
T ** sort(void) const
Convert our hash map into an alphabetically sorted linear object pointer array.
A common string class and character string support functions.
Create a linked list of auto-releasable objects.
This is a base class for objects that may be created in pager pools.
bool operator!() const
Check if can still save into buffer.
A smart pointer template for iterating linked lists.
bool assign(const char *name, void *pointer)
Assign a name to a data pointer.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
unsigned size(void) const
Get the size of a memory page.
Abstract interfaces and support.
~keypager()
Destroy the hash pager by purging the index chains and memory pools.
StringPager::member * begin(void) const
Get root of pager list.
const char * get(unsigned item) const
Get string item from list.
bool test(const char *name) const
Test if a name exists in the pool.
T * operator()(const char *name)
Reference a typed object directly by name.
DirPager dirlist_t
A convenience type for using DirPager directly.
String pager for storing lists of NULL terminated strings.
pager(mempager *heap=((void *) 0))
Construct a pager and optionally assign a private pager heap.
keypager(size_t size)
Create the object cache.
unsigned utilization(void)
Determine fragmentation level of acquired heap pages.
unsigned count(void) const
Get the number of items in the pager string list.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
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 linked object base class with members found by name.
unsigned max(void) const
Get the maximum number of pages that are permitted.
T * operator*()
Create a managed object by pointer reference.
StringPager & operator<<(const char *text)
Convenience operator to add to pager.
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.
static NamedObject * map(NamedObject **hash, const char *name, unsigned size)
Find a named object through a hash map table.
linked_pointer< T > iterator
Convenience typedef for iterative pointer.
A managed private heap for small allocations.
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.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
T * operator[](const char *name) const
Find a typed object derived from NamedObject in the hash map by name.
Various miscellaneous platform specific headers and defines.
const char * operator[](unsigned item) const
Return specified member from pager list.
unsigned count(void) const
Get the number of associations we have in our object.
void purge(void)
Purge the hash map of typed objects.
const char * pull(void)
Remove element from front of list.
T * operator()(void)
Create a managed object by casting reference.
Common character processing protocol.
static NamedObject * skip(NamedObject **hash, NamedObject *current, unsigned size)
Iterate through a hash map table.
Directory pager is a paged string list for directory file names.