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 181 182 183 184 185 186 187 188
|
/*
TiMidity++ -- MIDI to WAVE converter and player
Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#ifdef __POCC__
#include <sys/types.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifndef NO_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include "timidity.h"
#include "common.h"
#include "mblock.h"
static MBlockNode *free_mblock_list = NULL;
#define ADDRALIGN 8
/* #define DEBUG */
void init_mblock(MBlockList *mblock)
{
mblock->first = NULL;
mblock->allocated = 0;
}
static MBlockNode *new_mblock_node(size_t n)
{
MBlockNode *p;
if(n > MIN_MBLOCK_SIZE)
{
if((p = (MBlockNode *)safe_malloc(n + sizeof(MBlockNode))) == NULL)
return NULL;
p->block_size = n;
}
else if(free_mblock_list == NULL)
{
if((p = (MBlockNode *)safe_malloc(sizeof(MBlockNode)
+ MIN_MBLOCK_SIZE)) == NULL)
return NULL;
p->block_size = MIN_MBLOCK_SIZE;
}
else
{
p = free_mblock_list;
free_mblock_list = free_mblock_list->next;
}
p->offset = 0;
p->next = NULL;
return p;
}
static int enough_block_memory(MBlockList *mblock, size_t n)
{
size_t newoffset;
if(mblock->first == NULL)
return 0;
newoffset = mblock->first->offset + n;
if(newoffset < mblock->first->offset) /* exceed representable in size_t */
return 0;
if(newoffset > mblock->first->block_size)
return 0;
return 1;
}
void *new_segment(MBlockList *mblock, size_t nbytes)
{
MBlockNode *p;
void *addr;
/* round up to ADDRALIGN */
nbytes = ((nbytes + ADDRALIGN - 1) & ~(ADDRALIGN - 1));
if(!enough_block_memory(mblock, nbytes))
{
p = new_mblock_node(nbytes);
p->next = mblock->first;
mblock->first = p;
mblock->allocated += p->block_size;
}
else
p = mblock->first;
addr = (void *)(p->buffer + p->offset);
p->offset += nbytes;
#ifdef DEBUG
if(((unsigned long)addr) & (ADDRALIGN-1))
{
fprintf(stderr, "Bad address: 0x%x\n", addr);
exit(1);
}
#endif /* DEBUG */
return addr;
}
static void reuse_mblock1(MBlockNode *p)
{
if(p->block_size > MIN_MBLOCK_SIZE)
free(p);
else /* p->block_size <= MIN_MBLOCK_SIZE */
{
p->next = free_mblock_list;
free_mblock_list = p;
}
}
void reuse_mblock(MBlockList *mblock)
{
MBlockNode *p;
if((p = mblock->first) == NULL)
return; /* There is nothing to collect memory */
while(p)
{
MBlockNode *tmp;
tmp = p;
p = p->next;
reuse_mblock1(tmp);
}
init_mblock(mblock);
}
char *strdup_mblock(MBlockList *mblock, const char *str)
{
int len;
char *p;
len = strlen(str);
p = (char *)new_segment(mblock, len + 1); /* for '\0' */
memcpy(p, str, len + 1);
return p;
}
int free_global_mblock(void)
{
int cnt;
cnt = 0;
while(free_mblock_list)
{
MBlockNode *tmp;
tmp = free_mblock_list;
free_mblock_list = free_mblock_list->next;
free(tmp);
cnt++;
}
return cnt;
}
|