Cache dynamic mem

This commit is contained in:
2021-05-05 18:17:57 +02:00
parent cc29250d76
commit de44f4a3fe
2 changed files with 13 additions and 7 deletions

View File

@ -16,6 +16,7 @@
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <malloc.h>
int cache_continue = 1; int cache_continue = 1;
magic_t magic; magic_t magic;
@ -74,8 +75,8 @@ int cache_process() {
} }
FILE *file; FILE *file;
char buf[16384]; char *buf = malloc(CACHE_BUF_SIZE);
char comp_buf[16384]; char *comp_buf = malloc(CACHE_BUF_SIZE);
char filename_comp_gz[256]; char filename_comp_gz[256];
char filename_comp_br[256]; char filename_comp_br[256];
unsigned long read; unsigned long read;
@ -144,23 +145,23 @@ int cache_process() {
} }
} }
while ((read = fread(buf, 1, sizeof(buf), file)) > 0) { while ((read = fread(buf, 1, CACHE_BUF_SIZE, file)) > 0) {
SHA1_Update(&ctx, buf, read); SHA1_Update(&ctx, buf, read);
if (compress) { if (compress) {
unsigned long avail_in, avail_out; unsigned long avail_in, avail_out;
avail_in = read; avail_in = read;
do { do {
avail_out = sizeof(comp_buf); avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in, compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in,
comp_buf, &avail_out, feof(file)); comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, sizeof(comp_buf) - avail_out, comp_file_gz); fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_gz);
} while (avail_in != 0); } while (avail_in != 0);
avail_in = read; avail_in = read;
do { do {
avail_out = sizeof(comp_buf); avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in, compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in,
comp_buf, &avail_out, feof(file)); comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, sizeof(comp_buf) - avail_out, comp_file_br); fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_br);
} while (avail_in != 0); } while (avail_in != 0);
} }
} }
@ -193,6 +194,8 @@ int cache_process() {
cache_file = fopen("/var/necronda-server/cache", "wb"); cache_file = fopen("/var/necronda-server/cache", "wb");
if (cache_file == NULL) { if (cache_file == NULL) {
fprintf(stderr, ERR_STR "Unable to open cache file: %s" CLR_STR "\n", strerror(errno)); fprintf(stderr, ERR_STR "Unable to open cache file: %s" CLR_STR "\n", strerror(errno));
free(buf);
free(comp_buf);
return -1; return -1;
} }
fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file); fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
@ -201,6 +204,8 @@ int cache_process() {
sleep(1); sleep(1);
} }
} }
free(buf);
free(comp_buf);
return 0; return 0;
} }

View File

@ -12,6 +12,7 @@
#define CACHE_SHM_KEY 255641 #define CACHE_SHM_KEY 255641
#define CACHE_ENTRIES 1024 #define CACHE_ENTRIES 1024
#define CACHE_BUF_SIZE 16384
#ifndef CACHE_MAGIC_FILE #ifndef CACHE_MAGIC_FILE
# define CACHE_MAGIC_FILE "/usr/share/file/misc/magic.mgc" # define CACHE_MAGIC_FILE "/usr/share/file/misc/magic.mgc"