buffer_manage replaced for buffer_manager

This commit is contained in:
fc_botelho 2006-04-28 16:35:48 +00:00
parent 09ef0957fc
commit 124e0dfe68
4 changed files with 87 additions and 9 deletions

View File

@ -17,7 +17,7 @@ libcmph_la_SOURCES = debug.h\
chm.h chm_structs.h chm.c\ chm.h chm_structs.h chm.c\
bmz.h bmz_structs.h bmz.c\ bmz.h bmz_structs.h bmz.c\
bmz8.h bmz8_structs.h bmz8.c\ bmz8.h bmz8_structs.h bmz8.c\
buffer_manage.h buffer_manage.c\ buffer_manager.h buffer_manager.c\
buffer_entry.h buffer_entry.c\ buffer_entry.h buffer_entry.c\
brz.h brz_structs.h brz.c brz.h brz_structs.h brz.c

View File

@ -4,7 +4,7 @@
#include "brz.h" #include "brz.h"
#include "cmph_structs.h" #include "cmph_structs.h"
#include "brz_structs.h" #include "brz_structs.h"
#include "buffer_manage.h" #include "buffer_manager.h"
#include "cmph.h" #include "cmph.h"
#include "hash.h" #include "hash.h"
#include "bitbool.h" #include "bitbool.h"
@ -203,7 +203,7 @@ static int brz_gen_mphf(cmph_config_t *mph)
cmph_uint32 nflushes = 0; cmph_uint32 nflushes = 0;
cmph_uint32 h0; cmph_uint32 h0;
FILE * tmp_fd = NULL; FILE * tmp_fd = NULL;
buffer_manage_t * buff_manage = NULL; buffer_manager_t * buff_manager = NULL;
char *filename = NULL; char *filename = NULL;
char *key = NULL; char *key = NULL;
cmph_uint32 keylen; cmph_uint32 keylen;
@ -340,7 +340,7 @@ static int brz_gen_mphf(cmph_config_t *mph)
fwrite(brz->size, sizeof(cmph_uint8)*(brz->k), 1, brz->mphf_fd); fwrite(brz->size, sizeof(cmph_uint8)*(brz->k), 1, brz->mphf_fd);
//tmp_fds = (FILE **)calloc(nflushes, sizeof(FILE *)); //tmp_fds = (FILE **)calloc(nflushes, sizeof(FILE *));
buff_manage = buffer_manage_new(brz->memory_availability, nflushes); buff_manager = buffer_manager_new(brz->memory_availability, nflushes);
buffer_merge = (cmph_uint8 **)calloc(nflushes, sizeof(cmph_uint8 *)); buffer_merge = (cmph_uint8 **)calloc(nflushes, sizeof(cmph_uint8 *));
buffer_h0 = (cmph_uint32 *)calloc(nflushes, sizeof(cmph_uint32)); buffer_h0 = (cmph_uint32 *)calloc(nflushes, sizeof(cmph_uint32));
@ -349,10 +349,10 @@ static int brz_gen_mphf(cmph_config_t *mph)
{ {
filename = (char *)calloc(strlen((char *)(brz->tmp_dir)) + 11, sizeof(char)); filename = (char *)calloc(strlen((char *)(brz->tmp_dir)) + 11, sizeof(char));
sprintf(filename, "%s%u.cmph",brz->tmp_dir, i); sprintf(filename, "%s%u.cmph",brz->tmp_dir, i);
buffer_manage_open(buff_manage, i, filename); buffer_manager_open(buff_manager, i, filename);
free(filename); free(filename);
filename = NULL; filename = NULL;
key = (char *)buffer_manage_read_key(buff_manage, i); key = (char *)buffer_manager_read_key(buff_manager, i);
keylen = strlen(key); keylen = strlen(key);
h0 = hash(brz->h0, key, keylen) % brz->k; h0 = hash(brz->h0, key, keylen) % brz->k;
buffer_h0[i] = h0; buffer_h0[i] = h0;
@ -367,7 +367,7 @@ static int brz_gen_mphf(cmph_config_t *mph)
{ {
i = brz_min_index(buffer_h0, nflushes); i = brz_min_index(buffer_h0, nflushes);
cur_bucket = buffer_h0[i]; cur_bucket = buffer_h0[i];
key = (char *)buffer_manage_read_key(buff_manage, i); key = (char *)buffer_manager_read_key(buff_manager, i);
if(key) if(key)
{ {
while(key) while(key)
@ -378,7 +378,7 @@ static int brz_gen_mphf(cmph_config_t *mph)
keys_vd[nkeys_vd++] = key; keys_vd[nkeys_vd++] = key;
key = NULL; //transfer memory ownership key = NULL; //transfer memory ownership
e++; e++;
key = (char *)buffer_manage_read_key(buff_manage, i); key = (char *)buffer_manager_read_key(buff_manager, i);
} }
if (key) if (key)
{ {
@ -430,7 +430,7 @@ static int brz_gen_mphf(cmph_config_t *mph)
} }
} }
buffer_manage_destroy(buff_manage); buffer_manager_destroy(buff_manager);
free(keys_vd); free(keys_vd);
free(buffer_merge); free(buffer_merge);
free(buffer_h0); free(buffer_h0);

66
src/buffer_manager.c Normal file
View File

@ -0,0 +1,66 @@
#include "buffer_manager.h"
#include "buffer_entry.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
struct __buffer_manager_t
{
cmph_uint32 memory_avail; // memory available
buffer_entry_t ** buffer_entries; // buffer entries to be managed
cmph_uint32 nentries; // number of entries to be managed
cmph_uint32 *memory_avail_list; // memory available list
int pos_avail_list; // current position in memory available list
};
buffer_manager_t * buffer_manager_new(cmph_uint32 memory_avail, cmph_uint32 nentries)
{
cmph_uint32 memory_avail_entry, i;
buffer_manager_t *buff_manager = (buffer_manager_t *)malloc(sizeof(buffer_manager_t));
assert(buff_manager);
buff_manager->memory_avail = memory_avail;
buff_manager->buffer_entries = (buffer_entry_t **)calloc(nentries, sizeof(buffer_entry_t *));
buff_manager->memory_avail_list = (cmph_uint32 *)calloc(nentries, sizeof(cmph_uint32));
buff_manager->pos_avail_list = -1;
buff_manager->nentries = nentries;
memory_avail_entry = buff_manager->memory_avail/buff_manager->nentries + 1;
for(i = 0; i < buff_manager->nentries; i++)
{
buff_manager->buffer_entries[i] = buffer_entry_new(memory_avail_entry);
}
return buff_manager;
}
void buffer_manager_open(buffer_manager_t * buffer_manager, cmph_uint32 index, char * filename)
{
buffer_entry_open(buffer_manager->buffer_entries[index], filename);
}
cmph_uint8 * buffer_manager_read_key(buffer_manager_t * buffer_manager, cmph_uint32 index)
{
cmph_uint8 * key = NULL;
if (buffer_manager->pos_avail_list >= 0 ) // recovering memory
{
cmph_uint32 new_capacity = buffer_entry_get_capacity(buffer_manager->buffer_entries[index]) + buffer_manager->memory_avail_list[(buffer_manager->pos_avail_list)--];
buffer_entry_set_capacity(buffer_manager->buffer_entries[index], new_capacity);
//fprintf(stderr, "recovering memory\n");
}
key = buffer_entry_read_key(buffer_manager->buffer_entries[index]);
if (key == NULL) // storing memory to be recovered
{
buffer_manager->memory_avail_list[++(buffer_manager->pos_avail_list)] = buffer_entry_get_capacity(buffer_manager->buffer_entries[index]);
//fprintf(stderr, "storing memory to be recovered\n");
}
return key;
}
void buffer_manager_destroy(buffer_manager_t * buffer_manager)
{
cmph_uint32 i;
for(i = 0; i < buffer_manager->nentries; i++)
{
buffer_entry_destroy(buffer_manager->buffer_entries[i]);
}
free(buffer_manager->memory_avail_list);
free(buffer_manager->buffer_entries);
free(buffer_manager);
}

12
src/buffer_manager.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef __CMPH_BUFFER_MANAGE_H__
#define __CMPH_BUFFER_MANAGE_H__
#include "cmph_types.h"
#include <stdio.h>
typedef struct __buffer_manager_t buffer_manager_t;
buffer_manager_t * buffer_manager_new(cmph_uint32 memory_avail, cmph_uint32 nentries);
void buffer_manager_open(buffer_manager_t * buffer_manager, cmph_uint32 index, char * filename);
cmph_uint8 * buffer_manager_read_key(buffer_manager_t * buffer_manager, cmph_uint32 index);
void buffer_manager_destroy(buffer_manager_t * buffer_manager);
#endif