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  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
misc.cpp
Go to the documentation of this file.
1// Copyright (C) 2001-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3// Copyright (C) 2015 Cherokees of Idaho.
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18//
19// As a special exception, you may use this file as part of a free software
20// library without restriction. Specifically, if other files instantiate
21// templates or use macros or inline functions from this file, or you compile
22// this file and link it with other files to produce an executable, this
23// file does not by itself cause the resulting executable to be covered by
24// the GNU General Public License. This exception does not however
25// invalidate any other reasons why the executable file might be covered by
26// the GNU General Public License.
27//
28// This exception applies only to the code released under the name GNU
29// Common C++. If you copy code from other releases into a copy of GNU
30// Common C++, as the General Public License permits, the exception does
31// not apply to the code that you add in this way. To avoid misleading
32// anyone as to the status of such modified files, you must delete
33// this exception notice from them.
34//
35// If you write modifications of your own for GNU Common C++, it is your choice
36// whether to permit this exception to apply to your modifications.
37// If you do not wish that, delete this exception notice.
38//
39
40#include <ucommon-config.h>
41#include <commoncpp/config.h>
42#include <commoncpp/export.h>
43#include <commoncpp/thread.h>
44#include <commoncpp/exception.h>
45#include <commoncpp/misc.h>
46
47namespace ost {
48
49static unsigned getIndex(const char *id)
50{
51 unsigned idx = 0;
52 while(*id)
53 idx = (idx << 1) ^ (*(id++) & 0x1f);
54
55 return idx % KEYDATA_INDEX_SIZE;
56}
57
58char *MemPager::alloc(const char *str)
59{
60 size_t len = strlen(str) + 1;
61 char *cp = (char *)_alloc(len);
62 if(cp)
63 String::set(cp, len, str);
64
65 return cp;
66}
67
69{
70 clear();
71}
72
74{
75}
76
77void Assoc::clear(void)
78{
79 memset(&entries, 0, sizeof(entries));
80}
81
82void Assoc::setPointer(const char *id, void *data)
83{
84 unsigned idx = getIndex(id);
85 entry *e = (entry *)getMemory(sizeof(entry));
86 size_t size = strlen(id) + 1;
87 e->id = (const char *)getMemory(size);
88 String::set((char *)e->id, size, id);
89 e->data = data;
90 e->next = entries[idx];
91 entries[idx] = e;
92}
93
94void *Assoc::getPointer(const char *id) const
95{
96 entry *e = entries[getIndex(id)];
97
98 while(e) {
99 if(!stricmp(e->id, id))
100 break;
101 e = e->next;
102 }
103 if(e)
104 return e->data;
105 return NULL;
106}
107
109MemPager(pg), Mutex()
110{}
111
113{
114 enterMutex();
116 leaveMutex();
117}
118
119void *SharedMemPager::alloc(size_t size)
120{
121 void *mem;
122
123 enterMutex();
124 mem = MemPager::alloc(size);
125 leaveMutex();
126 return mem;
127}
128
129} // namespace ost
130
131/** EMACS **
132 * Local variables:
133 * mode: c++
134 * c-basic-offset: 4
135 * End:
136 */
virtual void * getMemory(size_t size)=0
void setPointer(const char *id, void *data)
Definition: misc.cpp:82
void * getPointer(const char *id) const
Definition: misc.cpp:94
virtual ~Assoc()
Definition: misc.cpp:73
void clear(void)
Definition: misc.cpp:77
entry * entries[97]
Definition: misc.h:152
Assoc()
Definition: misc.cpp:68
void * alloc(size_t size)
Definition: misc.h:73
void purge(void)
Definition: misc.h:91
void leaveMutex(void)
Definition: thread.h:74
void enterMutex(void)
Definition: thread.h:70
void * alloc(size_t size)
Get the last memory page after locking.
Definition: misc.cpp:119
SharedMemPager(size_t pagesize=4096)
Create a mempager mutex pool.
Definition: misc.cpp:108
void purge(void)
Purge the memory pool while locked.
Definition: misc.cpp:112
void set(const char *text)
Set string object to text of a null terminated string.
Definition: string.cpp:802
size_t size(void) const
Get the size of a memory page.
Definition: memory.h:125
virtual void * _alloc(size_t size) __OVERRIDE
Allocate memory from the pager heap.
Definition: memory.cpp:202
Export interfaces for library interfaces.
Common C++ thread class and sychronization objects.
int stricmp(const char *s1, const char *s2)
Definition: cpr.cpp:95
GNU Common C++ exception model base classes.
various miscellaneous classes historically used.
#define KEYDATA_INDEX_SIZE
Definition: misc.h:51
Definition: address.cpp:63
static unsigned getIndex(const char *id)
Definition: misc.cpp:49
String str(Socket &so, size_t size)
Definition: socket.cpp:3507
const char * id
Definition: misc.h:147
void * data
Definition: misc.h:149
entry * next
Definition: misc.h:148