irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

region.cpp
Go to the documentation of this file.
1 /* For copyright information please refer to files in the COPYRIGHT directory
2  */
3 #include "region.h"
4 #include <cstdlib>
5 #include <string.h>
6 #ifdef REGION_MALLOC
7 
8 Region *make_region( size_t is, jmp_buf *label ) {
9  Region *r = ( Region * )malloc( sizeof( Region ) );
10  if ( r == NULL ) {
11  return NULL;
12  }
13  struct region_node *node = ( struct region_node * )malloc( sizeof( struct region_node ) );
14  node->next = NULL;
15  node->ptr = NULL;
16  node->size = 0;
17  r->head = r->tail = node;
18  return r;
19 }
20 unsigned char *region_alloc_nodesc( Region *r, size_t s, size_t *alloc_size ) {
21  *alloc_size =
22  s > DEFAULT_BLOCK_SIZE ?
23  s :
24  roundToAlignment( s );
25  unsigned char *pointer = ( unsigned char * )malloc( *alloc_size );
26  struct region_node *node = ( struct region_node * )malloc( sizeof( struct region_node ) );
27  node->next = NULL;
28  node->ptr = pointer;
29  node->size = s;
30  r->tail->next = node;
31  r->tail = node;
32  return pointer;
33 
34 }
35 void *region_alloc( Region *r, size_t size ) {
36  size_t allocSize;
37  unsigned char *mem = region_alloc_nodesc( r, size + CACHE_SIZE( RegionDesc, 1 ), &allocSize );
38  ( ( RegionDesc * )mem )->region = r;
39  ( ( RegionDesc * )mem )->size = allocSize;
40  ( ( RegionDesc * )mem )->del = 0;
41  return mem + CACHE_SIZE( RegionDesc, 1 );
42 }
43 void region_free( Region *r ) {
44  while ( r->head != NULL ) {
45  struct region_node *node = r->head;
46  r->head = node->next;
47  memset( node->ptr, 0, node->size );
48  if ( node->ptr != NULL ) {
49  free( node->ptr );
50  }
51  free( node );
52  }
53  free( r );
54 }
55 size_t region_size( Region *r ) {
56  size_t s = 0;
57  struct region_node *node = r->head;
58  while ( node != NULL ) {
59  s += node->size;
60  node = node->next;
61  }
62  return s;
63 }
64 #else
65 /* utility function */
66 struct region_node *make_region_node( size_t is ) {
67  struct region_node *node = ( struct region_node * )malloc( sizeof( *node ) );
68  if ( node == NULL ) {
69  return NULL;
70  }
71 
72  node->block = ( unsigned char * )malloc( is );
73  memset( node->block, 0, is );
74  if ( node->block == NULL ) {
75  free( node );
76  return NULL;
77  }
78 
79  node->size = is;
80  node->used = 0;
81  node->next = NULL;
82 
83  return node;
84 }
85 Region *make_region( size_t is, jmp_buf *label ) {
86  Region *r = ( Region * )malloc( sizeof( Region ) );
87  if ( r == NULL ) {
88  return NULL;
89  }
90 
91  if ( is == 0 ) {
92  is = DEFAULT_BLOCK_SIZE;
93  }
94  struct region_node *node = make_region_node( is );
95  if ( node == NULL ) {
96  free( r );
97  return NULL;
98  }
99 
100  r->head = r->active = node;
101  r->label = label;
102  r->error.code = 0; /* set no error */
103  return r;
104 }
105 unsigned char *region_alloc_nodesc( Region *r, size_t s, size_t *alloc_size ) {
106  if ( s > r->active->size - r->active->used ) {
107  int blocksize;
108  if ( s > DEFAULT_BLOCK_SIZE ) {
109  blocksize = s;
110 
111  }
112  else {
113  blocksize = DEFAULT_BLOCK_SIZE;
114  }
115  struct region_node *next = make_region_node( blocksize );
116  if ( next == NULL ) {
117  if ( r->label == NULL ) { /* no error handler */
118  return NULL;
119  }
120  else { /* with error handler */
121  longjmp( *( r->label ), -1 );
122  }
123  }
124  r->active->next = next;
125  r->active = next;
126 
127  }
128 
129  *alloc_size =
130  s > DEFAULT_BLOCK_SIZE ?
131  s :
132  roundToAlignment( s );
133  unsigned char *pointer = r->active->block + r->active->used;
134  r->active->used += *alloc_size;
135  return pointer;
136 
137 }
138 void *region_alloc( Region *r, size_t size ) {
139  size_t allocSize;
140  unsigned char *mem = region_alloc_nodesc( r, size + CACHE_SIZE( RegionDesc, 1 ), &allocSize );
141  ( ( RegionDesc * )mem )->region = r;
142  ( ( RegionDesc * )mem )->size = allocSize;
143  ( ( RegionDesc * )mem )->del = 0;
144  return mem + CACHE_SIZE( RegionDesc, 1 );
145 }
146 void region_free( Region *r ) {
147  while ( r->head != NULL ) {
148  struct region_node *node = r->head;
149  r->head = node->next;
150  /* memset(node->block, 0, node->size); */
151  free( node->block );
152  free( node );
153  }
154  free( r->label );
155  free( r );
156 }
157 size_t region_size( Region *r ) {
158  size_t s = 0;
159  struct region_node *node = r->head;
160  while ( node != NULL ) {
161  s += node->used;
162  node = node->next;
163  }
164  return s;
165 }
166 #endif
167 
168 /* tests */
169 void assert( int res ) {
170  if ( !res ) {
171  printf( "error" );
172  }
173 }
NULL
#define NULL
Definition: rodsDef.h:70
region_node::block
unsigned char * block
Definition: region.h:37
make_region
Region * make_region(size_t is, jmp_buf *label)
Definition: region.cpp:85
region_node::size
size_t size
Definition: region.h:38
region_alloc_nodesc
unsigned char * region_alloc_nodesc(Region *r, size_t s, size_t *alloc_size)
Definition: region.cpp:105
irods.six.next
next
Definition: six.py:518
region_free
void region_free(Region *r)
Definition: region.cpp:146
node
Definition: restructs.hpp:244
region_error::code
int code
Definition: region.h:19
roundToAlignment
#define roundToAlignment(x)
Definition: region.h:12
pointer
Definition: parser.hpp:34
assert
void assert(int res)
Definition: region.cpp:169
region.h
DEFAULT_BLOCK_SIZE
#define DEFAULT_BLOCK_SIZE
Definition: region.h:9
region_node
Definition: region.h:36
region_size
size_t region_size(Region *r)
Definition: region.cpp:157
region::head
struct region_node * head
Definition: region.h:46
region
Definition: region.h:45
region::active
struct region_node * active
Definition: region.h:46
region_node::next
struct region_node * next
Definition: region.h:40
region_alloc
void * region_alloc(Region *r, size_t size)
Definition: region.cpp:138
region_node::used
size_t used
Definition: region.h:39
region::label
jmp_buf * label
Definition: region.h:47
region_desc
Definition: region.h:51
size
long long size
Definition: filesystem.cpp:102
label
Definition: restructs.hpp:288
CACHE_SIZE
#define CACHE_SIZE(t, l)
Definition: region.h:16
region::error
struct region_error error
Definition: region.h:48
make_region_node
struct region_node * make_region_node(size_t is)
Definition: region.cpp:66