aura  0.1
 All Data Structures Functions Variables Modules Pages
buffer.c
1 #include <aura/aura.h>
2 #include <aura/private.h>
3 
4 struct aura_buffer *aura_buffer_internal_request(int size);
5 void aura_buffer_internal_free(struct aura_buffer *buf);
6 
11 /* Let's see if we have a buffer in our pool here */
12 static struct aura_buffer *fetch_buffer_from_pool(struct aura_node *nd,
13  int size) {
14  struct aura_buffer *buf = NULL;
15  struct list_head *pos, *tmp;
16  list_for_each_safe(pos, tmp, &nd->buffer_pool)
17  {
18  buf = list_entry(pos, struct aura_buffer, qentry);
19  if (buf->size >= size) {
20  list_del(pos);
21  nd->num_buffers_in_pool--;
22  return buf;
23  }
24  }
25  return NULL ;
26 }
27 
28 
40 struct aura_buffer *aura_buffer_request(struct aura_node *nd, int size) {
41  struct aura_buffer *ret = NULL;
42  int act_size = size;
43  act_size += nd->tr->buffer_overhead;
44 
45 #ifdef AURA_USE_BUFFER_POOL
46  /* Try buffer pool first */
47  ret = fetch_buffer_from_pool(nd, act_size);
48  if (ret)
49  goto bailout; /* For the sake of readability */
50 #endif
51 
52  /* Fallback to alloc() */
53  if (!nd->tr->buffer_request) {
54  char *data = malloc(act_size + sizeof(struct aura_buffer));
55  ret = (struct aura_buffer *) data;
56  if (!ret)
57  BUG(nd, "FATAL: malloc() failed");
58  ret->data = &data[sizeof(*ret)];
59  } else {
60  ret = nd->tr->buffer_request(nd, act_size);
61  if (!ret)
62  BUG(nd, "FATAL: buffer allocation by transport failed");
63  }
64 
65 bailout:
66  ret->magic = AURA_BUFFER_MAGIC_ID;
67  ret->size = act_size;
68  ret->owner = nd;
69  aura_buffer_rewind(ret);
70  return ret;
71 }
72 
80 void aura_buffer_release(struct aura_buffer *buf) {
81  /* Just put the buffer back into the pool at the very start */
82 #ifdef AURA_USE_BUFFER_POOL
83  struct aura_node *nd = buf->owner;
84  if (buf->magic != AURA_BUFFER_MAGIC_ID)
85  BUG(nd,
86  "FATAL: Attempting to release a buffer with invalid magic OR double free an aura_buffer");
87 
88  list_add(&buf->qentry, &nd->buffer_pool);
89  nd->num_buffers_in_pool++;
90 #else
92 #endif
93 }
94 
101 void aura_buffer_destroy(struct aura_buffer *buf) {
102  struct aura_node *nd = buf->owner;
103  if (buf->magic != AURA_BUFFER_MAGIC_ID)
104  BUG(nd,
105  "FATAL: Attempting to destroy a buffer with invalid magic OR double free an aura_buffer");
106  buf->magic = 0;
107 
108  if (nd && nd->tr->buffer_release)
109  nd->tr->buffer_release(buf);
110  else
111  free(buf);
112 }
113 
124 void aura_bufferpool_gc(struct aura_node *nd, int numdrop, int threshold) {
125  struct aura_buffer *pos, *tmp;
126  /* We iterate in reverse order, since the least used buffers
127  * will naturally end up at the very end of the list
128  */
129  list_for_each_entry_safe_reverse(pos, tmp, &nd->buffer_pool, qentry)
130  {
131  if ((numdrop == -1)
132  || (numdrop-- && nd->num_buffers_in_pool > threshold)) {
133  list_del(&pos->qentry);
134  nd->num_buffers_in_pool--;
135  aura_buffer_destroy(pos);
136  } else {
137  return; /* Done for now */
138  }
139  }
140 }
141 
147 void aura_bufferpool_preheat(struct aura_node *nd, int size, int count)
148 {
149  while (count--) {
150  struct aura_buffer *buf = aura_buffer_request(nd, size);
151  aura_buffer_release(buf);
152  slog(0, SLOG_DEBUG, "!");
153  }
154 }
155 
164 void aura_bufferpool_set_gc_threshold(struct aura_node *nd, int threshold) {
165  nd->gc_threshold = threshold;
166 }
167 
struct aura_buffer * aura_buffer_request(struct aura_node *nd, int size)
Definition: buffer.c:40
void aura_bufferpool_set_gc_threshold(struct aura_node *nd, int threshold)
Definition: buffer.c:164
void aura_bufferpool_gc(struct aura_node *nd, int numdrop, int threshold)
Definition: buffer.c:124
uint32_t magic
Definition: aura.h:335
struct aura_node * owner
Definition: aura.h:343
Definition: list.h:61
struct list_head qentry
Definition: aura.h:345
int pos
Definition: aura.h:339
void aura_bufferpool_preheat(struct aura_node *nd, int size, int count)
Definition: buffer.c:147
static void aura_buffer_rewind(struct aura_buffer *buf)
Definition: inlines.h:93
int size
Definition: aura.h:337
void aura_buffer_release(struct aura_buffer *buf)
Definition: buffer.c:80
void aura_buffer_destroy(struct aura_buffer *buf)
Definition: buffer.c:101
char * data
Definition: aura.h:347