aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/memorypool.c
blob: d8e835e8e0e7045c1356f48660b83b6c30f7e9e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
     This file is part of libmicrohttpd
     (C) 2007 Daniel Pittman and Christian Grothoff

     libmicrohttpd is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     libmicrohttpd is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libmicrohttpd; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file memorypool.c
 * @brief memory pool
 * @author Christian Grothoff
 */

#include "memorypool.h"

struct MemoryPool {

  /**
   * Pointer to the pool's memory
   */ 
  char * memory;

  /**
   * Size of the pool.
   */
  unsigned int size;

  /**
   * Offset of the first unallocated byte.
   */
  unsigned int pos;

  /**
   * Offset of the last unallocated byte.
   */
  unsigned int end;

  /**
   * 0 if pool was malloc'ed, 1 if mmapped.
   */
  int is_mmap;
};

/**
 * Create a memory pool.
 * 
 * @param max maximum size of the pool
 */
struct MemoryPool * MHD_pool_create(unsigned int max) {
  struct MemoryPool * pool;

  pool = malloc(sizeof(struct MemoryPool));
  if (pool == NULL)
    return NULL;
  pool->memory = MMAP(NULL, max, PROT_READ | PROT_WRITE,
		      MAP_ANONYMOUS, -1, 0);
  if ( (pool->memory == MAP_FAILED) ||
       (pool->memory == NULL) ) {
    pool->memory = malloc(max);
    if (pool->memory == NULL) {
      free(pool);
      return NULL;
    }
    pool->is_mmap = 0;
  } else {
    pool->is_mmap = 1;
  }	   
  pool->pos = 0;
  pool->end = max;
  pool->size = max;
  return pool;
}

/**
 * Destroy a memory pool.
 */
void MHD_pool_destroy(struct MemoryPool * pool) {
  if (pool == NULL)
    return;
  if (pool->is_mmap == 0)
    free(pool->memory);
  else
    MUNMAP(pool->memory, pool->size);
  free(pool);
}

/**
 * Allocate size bytes from the pool.
 * @return NULL if the pool cannot support size more
 *         bytes
 */
void * MHD_pool_allocate(struct MemoryPool * pool,
			 unsigned int size,
			 int from_end) {
  void * ret;

  if ( (pool->pos + size > pool->end) ||
       (pool->pos + size < pool->pos) )
    return NULL;
  if (from_end == MHD_YES) {
    ret = &pool->memory[pool->end - size];
    pool->end -= size;
  } else {
    ret = &pool->memory[pool->pos];
    pool->pos += size;
  }
  return ret;
}

/**
 * Reallocate a block of memory obtained from the pool.
 * This is particularly efficient when growing or
 * shrinking the block that was last (re)allocated.
 * If the given block is not the most recenlty 
 * (re)allocated block, the memory of the previous
 * allocation may be leaked until the pool is 
 * destroyed (and copying the data maybe required).
 *
 * @param old the existing block
 * @param old_size the size of the existing block
 * @param new_size the new size of the block
 * @return new address of the block, or 
 *         NULL if the pool cannot support new_size 
 *         bytes (old continues to be valid for old_size)
 */
void * MHD_pool_reallocate(struct MemoryPool * pool,
			   void * old,
			   unsigned int old_size,
			   unsigned int new_size) {  
  void * ret;

  if ( (pool->end < old_size) ||
       (pool->end < new_size) ) 
    return NULL; /* unsatisfiable or bogus request */

  if ( (pool->pos >= old_size) &&
       (&pool->memory[pool->pos - old_size] == old) ) {
    /* was the previous allocation - optimize! */
    if (pool->pos + new_size - old_size <= pool->end) {
      /* fits */
      pool->pos += new_size - old_size;
      if (new_size < old_size) /* shrinking - zero again! */
	memset(&pool->memory[pool->pos],
	       0,
	       old_size - new_size);
      return old;
    }
    /* does not fit */
    return NULL;    
  }
  if (new_size <= old_size)
    return old; /* cannot shrink, no need to move */
  if ( (pool->pos + new_size >= pool->pos) &&
       (pool->pos + new_size <= pool->end) ) {
    /* fits */
    ret = &pool->memory[pool->pos];
    memcpy(ret,
	   old,
	   old_size);
    pool->pos += new_size;
    return ret;
  }
  /* does not fit */
  return NULL;
}

/* end of memorypool.c */