ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

memory.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
31 #ifndef _UCOMMON_MEMORY_H_
32 #define _UCOMMON_MEMORY_H_
33 
34 #ifndef _UCOMMON_CONFIG_H_
35 #include <ucommon/platform.h>
36 #endif
37 
38 #ifndef _UCOMMON_PROTOCOLS_H_
39 #include <ucommon/protocols.h>
40 #endif
41 
42 #ifndef _UCOMMON_LINKED_H_
43 #include <ucommon/linked.h>
44 #endif
45 
46 #ifndef _UCOMMON_STRING_H_
47 #include <ucommon/string.h>
48 #endif
49 
50 namespace ucommon {
51 
52 class PagerPool;
53 
62 {
63 private:
64  friend class bufpager;
65 
66  size_t pagesize, align;
67  unsigned count;
68 
69  typedef struct mempage {
70  struct mempage *next;
71  union {
72  void *memalign;
73  unsigned used;
74  };
75  } page_t;
76 
78 
79 protected:
80  unsigned limit;
81 
86  page_t *pager(void);
87 
88 public:
93  memalloc(size_t page = 0);
94 
95  memalloc(const memalloc& copy);
96 
100  virtual ~memalloc();
101 
106  inline unsigned pages(void) const {
107  return count;
108  }
109 
117  inline unsigned max(void) const {
118  return limit;
119  }
120 
125  inline size_t size(void) const {
126  return pagesize;
127  }
128 
139  unsigned utilization(void) const;
140 
144  void purge(void);
145 
146 protected:
154  virtual void *_alloc(size_t size) __OVERRIDE;
155 
156 public:
161  void assign(memalloc& source);
162 };
163 
185 {
186 private:
187  mutable pthread_mutex_t mutex;
188 
189 protected:
196  virtual void _lock(void) __OVERRIDE;
197 
201  virtual void _unlock(void) __OVERRIDE;
202 
203 public:
208  mempager(size_t page = 0);
209 
210  mempager(const mempager& copy);
211 
215  virtual ~mempager();
216 
227  unsigned utilization(void);
228 
232  void purge(void);
233 
241  virtual void dealloc(void *memory);
242 
243 protected:
252  virtual void *_alloc(size_t size) __OVERRIDE;
253 
254 public:
259  void assign(mempager& source);
260 };
261 
262 class __EXPORT ObjectPager : protected memalloc
263 {
264 public:
265  class __EXPORT member : public LinkedObject
266  {
267  private:
268  void *mem;
269 
270  protected:
271  friend class ObjectPager;
272 
273  inline void set(member *node) {
274  Next = node;
275  }
276 
277  inline void *get(void) const {
278  return mem;
279  }
280 
281  member(LinkedObject **root);
282  member();
283 
284  public:
285  inline void *operator*() const {
286  return mem;
287  }
288  };
289 
290 private:
291  unsigned members;
293  size_t typesize;
295  void **index;
296 
298 
299 protected:
300  ObjectPager(size_t objsize, size_t pagesize = 256);
301 
308  void *get(unsigned item) const;
309 
314  void *add(void);
315 
316  void *push(void);
317 
322  void *pull(void);
323 
328  void *pop(void);
329 
334  void *invalid(void) const;
335 
336 public:
341  void clear(void);
342 
348  inline ObjectPager::member *begin(void) {
349  return static_cast<ObjectPager::member *>(root);
350  }
351 
352  inline operator bool() const {
353  return members > 0;
354  }
355 
356  inline bool operator!() const {
357  return !members;
358  }
359 
364  inline unsigned count(void) const {
365  return members;
366  }
367 
372 
373  inline size_t size(void) {
374  return memalloc::size();
375  }
376 
377  inline unsigned pages(void) {
378  return memalloc::pages();
379  }
380 
381 protected:
386  void **list(void);
387 
388 public:
393  void assign(ObjectPager& source);
394 };
395 
401 class __EXPORT StringPager : protected memalloc
402 {
403 private:
404  unsigned members;
406 
407 public:
415  virtual bool filter(char *text, size_t size);
416 
423  class __EXPORT member : public LinkedObject
424  {
425  private:
426  const char *text;
427 
428  protected:
429  friend class StringPager;
430 
431  inline void set(member *node)
432  {Next = node;}
433 
434  member(LinkedObject **root, const char *data);
435  member(const char *data);
436 
437  public:
438  inline const char *operator*() const {
439  return text;
440  }
441 
442  inline const char *get(void) const {
443  return text;
444  }
445  };
446 
451  StringPager(size_t pagesize = 256);
452 
453  StringPager(char **list, size_t pagesize = 256);
454 
459  inline unsigned count(void) const {
460  return members;
461  }
462 
469  const char *get(unsigned item) const;
470 
476  void set(unsigned item, const char *string);
477 
482  void add(const char *text);
483 
488  void push(const char *text);
489 
494  void push(char **text);
495 
500  const char *pull(void);
501 
506  const char *pop(void);
507 
513  void add(char **list);
514 
520  void set(char **list);
521 
526  void clear(void);
527 
534  inline const char *operator[](unsigned item) const {
535  return get(item);
536  }
537 
538  inline const char *at(unsigned item) const {
539  return get(item);
540  }
541 
547  inline StringPager::member *begin(void) const {
548  return static_cast<StringPager::member *>(root);
549  }
550 
555  inline void operator+=(const char *text) {
556  add(text);
557  }
558 
563  inline StringPager& operator<<(const char *text) {
564  add(text);
565  return *this;
566  }
567 
568  inline StringPager& operator>>(const char *text) {
569  push(text);
570  return *this;
571  }
572 
576  void sort(void);
577 
582  char **list(void);
583 
592  unsigned token(const char *text, const char *list, const char *quote = NULL, const char *end = NULL);
593 
594  unsigned split(const char *text, const char *string, unsigned flags = 0);
595 
596  unsigned split(stringex_t& expr, const char *string, unsigned flags = 0);
597 
598  String join(const char *prefix = NULL, const char *middle = NULL, const char *suffix = NULL);
599 
600  inline operator bool() const {
601  return members > 0;
602  }
603 
604  inline bool operator!() const {
605  return !members;
606  }
607 
608  inline StringPager& operator=(char **list) {
609  set(list);
610  return *this;
611  }
612 
613  inline const char *operator*() {
614  return pull();
615  }
616 
617  inline operator char **() {
618  return list();
619  }
620 
625 
626  inline size_t size(void) const {
627  return memalloc::size();
628  }
629 
630  inline unsigned pages(void) const {
631  return memalloc::pages();
632  }
633 
634 private:
636  char **index;
637 
638 public:
643  void assign(StringPager& source);
644 };
645 
653 class __EXPORT DirPager : protected StringPager
654 {
655 private:
657 
658 protected:
659  const char *dir;
660 
668  virtual bool filter(char *filename, size_t size) __OVERRIDE;
669 
675  bool load(const char *path);
676 
677 public:
678  DirPager();
679 
680  DirPager(const char *path);
681 
682  void operator=(const char *path);
683 
684  inline const char *operator*() const {
685  return dir;
686  }
687 
688  inline operator bool() const {
689  return dir != NULL;
690  }
691 
692  inline bool operator!() const {
693  return dir == NULL;
694  }
695 
696  inline unsigned count(void) const {
697  return StringPager::count();
698  }
699 
706  inline const char *operator[](unsigned item) const {
707  return StringPager::get(item);
708  }
709 
710  inline const char *get(unsigned item) const {
711  return StringPager::get(item);
712  }
713 
714  inline const char *at(unsigned item) const {
715  return StringPager::get(item);
716  }
717 
718  inline size_t size(void) const {
719  return memalloc::size();
720  }
721 
722  inline unsigned pages(void) const {
723  return memalloc::pages();
724  }
725 
726 public:
731  void assign(DirPager& source);
732 };
733 
742 {
743 private:
745 
747 
748 public:
752  autorelease();
753 
757  ~autorelease();
758 
764  void release(void);
765 
770  void operator+=(LinkedObject *object);
771 };
772 
784 {
785 private:
787 
788 protected:
789  friend class PagerPool;
790 
792 
796  PagerObject();
797 
801  void reset(void);
802 
803  void retain(void) __OVERRIDE;
804 
808  void release(void) __OVERRIDE;
809 
813  void dealloc(void) __OVERRIDE;
814 };
815 
825 {
826 private:
828  mutable pthread_mutex_t mutex;
829 
831 
832 protected:
833  PagerPool();
834  virtual ~PagerPool();
835 
836  PagerObject *get(size_t size);
837 
838 public:
843  void put(PagerObject *object);
844 };
845 
852 template <typename T>
853 class pager : private MemoryRedirect, private PagerPool
854 {
855 private:
857 
858 public:
863  inline pager(mempager *heap = NULL) : MemoryRedirect(heap), PagerPool() {}
864 
869  inline T *operator()(void) {
870  return new(get(sizeof(T))) T;
871  }
872 
877  inline T *operator*() {
878  return new(get(sizeof(T))) T;
879  }
880 };
881 
886 
891 
896 
897 inline String str(StringPager& list, const char *prefix = NULL, const char *middle = NULL, const char *suffix = NULL) {
898  return list.join(prefix, middle, suffix);
899 }
900 
901 } // namespace ucommon
902 
903 #endif
ucommon::mempager
Definition: memory.h:184
ucommon::ObjectPager::size
size_t size(void)
Definition: memory.h:373
ucommon::memalloc::mempage
Definition: memory.h:69
ucommon::ObjectPager::member::operator*
void * operator*() const
Definition: memory.h:285
ucommon::StringPager::begin
StringPager::member * begin(void) const
Definition: memory.h:547
ucommon::ObjectPager::member::mem
void * mem
Definition: memory.h:268
ucommon::ObjectPager::member::get
void * get(void) const
Definition: memory.h:277
ucommon::DirPager::size
size_t size(void) const
Definition: memory.h:718
ucommon::limit
T &() limit(T &value, T &low, T &high)
Definition: generics.h:468
ucommon::PagerPool::mutex
pthread_mutex_t mutex
Definition: memory.h:828
ucommon::stringlistitem_t
StringPager::member stringlistitem_t
Definition: memory.h:890
ucommon::PagerPool
Definition: memory.h:824
ucommon::ObjectPager::member::set
void set(member *node)
Definition: memory.h:273
ucommon::PagerObject::pager
PagerPool * pager
Definition: memory.h:791
ucommon
Definition: access.cpp:23
ucommon::pager::operator*
T * operator*()
Definition: memory.h:877
ucommon::memalloc::pages
unsigned pages(void) const
Definition: memory.h:106
ucommon::StringPager::operator*
const char * operator*()
Definition: memory.h:613
ucommon::StringPager::operator!
bool operator!() const
Definition: memory.h:604
ucommon::ObjectPager::begin
ObjectPager::member * begin(void)
Definition: memory.h:348
ucommon::memalloc::size
size_t size(void) const
Definition: memory.h:125
protocols.h
ucommon::StringPager::operator+=
void operator+=(const char *text)
Definition: memory.h:555
ucommon::memalloc::page
page_t * page
Definition: memory.h:77
__OVERRIDE
#define __OVERRIDE
Definition: platform.h:158
ucommon::String
Definition: string.h:78
ucommon::MemoryRedirect
Definition: protocols.h:100
ucommon::StringPager::get
const char * get(unsigned item) const
Definition: memory.cpp:620
ucommon::memalloc::mempage::next
struct mempage * next
Definition: memory.h:70
ucommon::memalloc::mempage::used
unsigned used
Definition: memory.h:73
ucommon::ObjectPager::count
unsigned count(void) const
Definition: memory.h:364
ucommon::StringPager
Definition: memory.h:401
ucommon::stringlist_t
StringPager stringlist_t
Definition: memory.h:885
ucommon::copy
T copy(const T &src)
Definition: generics.h:395
ucommon::pager::pager
pager(mempager *heap=NULL)
Definition: memory.h:863
ucommon::ObjectPager
Definition: memory.h:262
ucommon::memalloc::max
unsigned max(void) const
Definition: memory.h:117
ucommon::StringPager::member::set
void set(member *node)
Definition: memory.h:431
ucommon::StringPager::join
String join(const char *prefix=NULL, const char *middle=NULL, const char *suffix=NULL)
Definition: memory.cpp:532
ucommon::PagerObject
Definition: memory.h:783
ucommon::clear
T & clear(T &o)
Definition: generics.h:416
ucommon::autorelease
Definition: memory.h:741
ucommon::LockingProtocol
Definition: protocols.h:118
quote
static shell::stringopt quote('q', "--quote", _TEXT("set quote for each argument"), "string", "")
ucommon::memalloc::pagesize
size_t pagesize
Definition: memory.h:66
ucommon::ObjectPager::members
unsigned members
Definition: memory.h:291
ucommon::StringPager::member::text
const char * text
Definition: memory.h:426
ucommon::mempager::mutex
pthread_mutex_t mutex
Definition: memory.h:187
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::MemoryProtocol
Definition: protocols.h:43
ucommon::dirlist_t
DirPager dirlist_t
Definition: memory.h:895
ucommon::StringPager::operator[]
const char * operator[](unsigned item) const
Definition: memory.h:534
ucommon::StringPager::member
Definition: memory.h:423
ucommon::DirPager::operator*
const char * operator*() const
Definition: memory.h:684
ucommon::pager
Definition: memory.h:853
ucommon::ObjectPager::last
member * last
Definition: memory.h:294
ucommon::StringPager::pages
unsigned pages(void) const
Definition: memory.h:630
ucommon::StringPager::at
const char * at(unsigned item) const
Definition: memory.h:538
ucommon::DirPager::count
unsigned count(void) const
Definition: memory.h:696
ucommon::DirPager::operator!
bool operator!() const
Definition: memory.h:692
ucommon::linked_pointer
Definition: linked.h:991
ucommon::StringPager::operator<<
StringPager & operator<<(const char *text)
Definition: memory.h:563
ucommon::StringPager::operator>>
StringPager & operator>>(const char *text)
Definition: memory.h:568
ucommon::pager::operator()
T * operator()(void)
Definition: memory.h:869
ucommon::DirPager::operator[]
const char * operator[](unsigned item) const
Definition: memory.h:706
ucommon::PagerPool::freelist
LinkedObject * freelist
Definition: memory.h:827
ucommon::String::regex
Definition: string.h:98
ucommon::StringPager::count
unsigned count(void) const
Definition: memory.h:459
__DELETE_COPY
#define __DELETE_COPY(x)
Definition: platform.h:160
platform.h
ucommon::StringPager::last
member * last
Definition: memory.h:635
ucommon::StringPager::operator=
StringPager & operator=(char **list)
Definition: memory.h:608
ucommon::ObjectPager::root
LinkedObject * root
Definition: memory.h:292
ucommon::LinkedObject
Definition: linked.h:55
ucommon::dir
Definition: fsys.h:743
ucommon::ObjectPager::operator!
bool operator!() const
Definition: memory.h:356
ucommon::str
String str(Socket &so, size_t size)
Definition: socket.cpp:3507
ucommon::ObjectPager::pages
unsigned pages(void)
Definition: memory.h:377
ucommon::DirPager
Definition: memory.h:653
ucommon::ObjectPager::typesize
size_t typesize
Definition: memory.h:293
ucommon::memalloc
Definition: memory.h:61
ucommon::StringPager::member::operator*
const char * operator*() const
Definition: memory.h:438
ucommon::StringPager::member::get
const char * get(void) const
Definition: memory.h:442
string.h
ucommon::pager::__DELETE_COPY
__DELETE_COPY(pager)
ucommon::DirPager::get
const char * get(unsigned item) const
Definition: memory.h:710
ucommon::autorelease::pool
LinkedObject * pool
Definition: memory.h:744
prefix
static char prefix[80]
Definition: args.cpp:33
ucommon::StringPager::index
char ** index
Definition: memory.h:636
__PROTOCOL
#define __PROTOCOL
Definition: platform.h:112
ucommon::PagerPool::get
PagerObject * get(size_t size)
Definition: memory.cpp:946
ucommon::StringPager::root
LinkedObject * root
Definition: memory.h:405
ucommon::ObjectPager::member
Definition: memory.h:265
ucommon::DirPager::dir
const char * dir
Definition: memory.h:659
ucommon::StringPager::size
size_t size(void) const
Definition: memory.h:626
ucommon::ObjectPager::index
void ** index
Definition: memory.h:295
ucommon::DirPager::pages
unsigned pages(void) const
Definition: memory.h:722
ucommon::memalloc::count
unsigned count
Definition: memory.h:67
ucommon::operator+=
std::string & operator+=(std::string &target, String &source)
Definition: stream.h:581
ucommon::StringPager::iterator
linked_pointer< StringPager::member > iterator
Definition: memory.h:624
ucommon::StringPager::members
unsigned members
Definition: memory.h:404
suffix
static char suffix[80]
Definition: args.cpp:34
ucommon::ObjectPager::iterator
linked_pointer< ObjectPager::member > iterator
Definition: memory.h:371
ucommon::DirPager::at
const char * at(unsigned item) const
Definition: memory.h:714
ucommon::memalloc::limit
unsigned limit
Definition: memory.h:80
linked.h
ucommon::CountedObject
Definition: object.h:56
ucommon::memalloc::mempage::memalign
void * memalign
Definition: memory.h:72