<libroxml  version="3.0.1" />
contact: tristan.lelong@libroxml.net
roxml_mem.c
Go to the documentation of this file.
1 
12 #include <stdlib.h>
13 #include "roxml_mem.h"
14 
16 memory_cell_t head_cell = { PTR_NONE, 0, NULL, 0, NULL, NULL };
17 
18 ROXML_STATIC ROXML_INT inline void roxml_release_last(void *data)
19 {
20  memory_cell_t *ptr = &head_cell;
21  memory_cell_t *to_delete = NULL;
22 
23  while ((ptr->prev != NULL) && (ptr->prev->id != pthread_self()))
24  ptr = ptr->prev;
25 
26  if (ptr->prev == NULL)
27  return;
28 
29  to_delete = ptr->prev;
30 
31  if (to_delete->next) {
32  to_delete->prev->next = to_delete->next;
33  to_delete->next->prev = to_delete->prev;
34  } else {
35  if (to_delete->prev != &head_cell)
36  head_cell.prev = to_delete->prev;
37  else
38  head_cell.prev = NULL;
39  to_delete->prev->next = NULL;
40  }
41 
42  if (PTR_IS_STAR(to_delete)) {
43  int i = 0;
44  for (i = 0; i < to_delete->occ; i++)
45  free(((void **)(to_delete->ptr))[i]);
46  }
47  if (to_delete->type != PTR_NONE) {
48  free(to_delete->ptr);
49  to_delete->type = PTR_NONE;
50  free(to_delete);
51  }
52 }
53 
54 ROXML_STATIC ROXML_INT inline void roxml_release_all(void *data)
55 {
56  memory_cell_t *to_delete = NULL;
57 
58  head_cell.prev = NULL;
59 
60  while ((head_cell.next != NULL)) {
61  to_delete = head_cell.next;
62  if (to_delete->next)
63  to_delete->next->prev = &head_cell;
64  head_cell.next = to_delete->next;
65 
66  if (PTR_IS_STAR(to_delete)) {
67  int i = 0;
68  for (i = 0; i < to_delete->occ; i++)
69  free(((void **)(to_delete->ptr))[i]);
70  }
71  free(to_delete->ptr);
72  to_delete->ptr = NULL;
73  to_delete->type = PTR_NONE;
74  free(to_delete);
75  }
76 }
77 
78 ROXML_STATIC ROXML_INT inline void roxml_release_pointer(void *data)
79 {
80  memory_cell_t *ptr = &head_cell;
81  memory_cell_t *to_delete = NULL;
82 
83  while ((ptr->next != NULL) && (ptr->next->ptr != data))
84  ptr = ptr->next;
85 
86  if (ptr->next == NULL)
87  return;
88 
89  to_delete = ptr->next;
90  if (to_delete->next) {
91  to_delete->next->prev = ptr;
92  } else {
93  if (ptr == &head_cell)
94  head_cell.prev = NULL;
95  else
96  head_cell.prev = to_delete->prev;
97  }
98  ptr->next = to_delete->next;
99  if (PTR_IS_STAR(to_delete)) {
100  int i = 0;
101  for (i = 0; i < to_delete->occ; i++)
102  free(((void **)(to_delete->ptr))[i]);
103  }
104  free(to_delete->ptr);
105  to_delete->type = PTR_NONE;
106  free(to_delete);
107 }
108 
109 ROXML_API void roxml_release(void *data)
110 {
111  if (data == RELEASE_LAST)
112  roxml_release_last(data);
113  else if (data == RELEASE_ALL)
114  roxml_release_all(data);
115  else
116  roxml_release_pointer(data);
117 
118  if (head_cell.next == &head_cell)
119  head_cell.next = NULL;
120  if (head_cell.prev == &head_cell)
121  head_cell.prev = NULL;
122 }
123 
124 ROXML_INT void *roxml_malloc(int size, int num, int type)
125 {
126  memory_cell_t *cell = &head_cell;
127  while (cell->next != NULL)
128  cell = cell->next;
129 
130  cell->next = malloc(sizeof(memory_cell_t));
131  if (!cell->next)
132  return NULL;
133 
134  cell->next->next = NULL;
135  cell->next->prev = cell;
136  cell = cell->next;
137  cell->type = type;
138  cell->id = pthread_self();
139  cell->occ = size;
140  cell->ptr = calloc(num, size);
141  head_cell.prev = cell;
142 
143  return cell->ptr;
144 }
unsigned long int id
Definition: roxml_types.h:44
#define RELEASE_LAST
Definition: roxml.h:188
#define PTR_IS_STAR(a)
memory cell structure
Definition: roxml_types.h:40
#define ROXML_API
Definition: roxml.h:24
memory_cell_t head_cell
head of memory manager
Definition: roxml_mem.c:16
ROXML_API void roxml_release(void *data)
memory cleanning function
Definition: roxml_mem.c:109
struct memory_cell * prev
Definition: roxml_types.h:46
XML internal memory management module.
struct memory_cell * next
Definition: roxml_types.h:45
#define PTR_NONE
ROXML_INT void * roxml_malloc(int size, int num, int type)
alloc memory function
Definition: roxml_mem.c:124
#define RELEASE_ALL
Definition: roxml.h:158