memory.c
3.02 KB
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
/*
* Copyright (c) 1999-2003 Caucho Technology. All rights reserved.
*
* This file is part of Resin(R) Open Source
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Resin Open Source 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 of the License, or
* (at your option) any later version.
*
* Resin Open Source 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, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Resin Open Source; if not, write to the
* Free SoftwareFoundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#endif
#include "cse.h"
#define BLOCK_SIZE 32 * 1024
typedef struct pool_block_t {
struct pool_block_t *next;
char *buffer;
int size;
int offset;
} pool_block_t;
struct mem_pool_t {
struct config_t *config;
struct pool_block_t *block;
void *lock;
};
mem_pool_t *
cse_create_pool(config_t *config)
{
mem_pool_t *pool = (mem_pool_t *) cse_malloc(sizeof(mem_pool_t));
memset(pool, 0, sizeof(mem_pool_t));
pool->config = config;
pool->lock = cse_create_lock(config);
LOG(("%s:%d:cse_create_pool(): memory lock %p\n",
__FILE__, __LINE__, pool->lock));
LOG(("%s:%d:cse_create_pool(): create pool %p\n",
__FILE__, __LINE__, pool));
return pool;
}
void *
cse_alloc(mem_pool_t *pool, int size)
{
void *data;
int frag = size % 8;
if (frag > 0)
size += 8 - frag;
cse_lock(pool->lock);
if (! pool->block || pool->block->size - pool->block->offset < size) {
pool_block_t *block = (pool_block_t *) cse_malloc(sizeof(pool_block_t));
memset(block, 0, sizeof(pool_block_t));
block->next = pool->block;
pool->block = block;
if (size > BLOCK_SIZE)
block->size = size;
else
block->size = BLOCK_SIZE;
block->buffer = cse_malloc(block->size);
}
data = pool->block->buffer + pool->block->offset;
pool->block->offset += size;
cse_unlock(pool->lock);
return data;
}
char *
cse_strdup(mem_pool_t *pool, const char *str)
{
int len = strlen(str);
char *buf = cse_alloc(pool, len + 1);
strcpy(buf, str);
return buf;
}
void
cse_free_pool(mem_pool_t *pool)
{
pool_block_t *ptr;
pool_block_t *next;
LOG(("%s:%d:cse_free_pool(): free pool %p\n", __FILE__, __LINE__, pool));
cse_lock(pool->lock);
for (ptr = pool->block; ptr; ptr = next) {
next = ptr->next;
cse_free(ptr->buffer);
cse_free(ptr);
}
pool->block = 0;
cse_unlock(pool->lock);
cse_free_lock(pool->config, pool->lock);
pool->lock = 0;
cse_free(pool);
}