-
Notifications
You must be signed in to change notification settings - Fork 41
/
acism_file.c
95 lines (85 loc) · 2.43 KB
/
acism_file.c
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
/*
** Copyright (C) 2009-2014 Mischa Sandberg <mischasan@gmail.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License Version 3 as
** published by the Free Software Foundation. You may not use, modify or
** distribute this program under any other version of the GNU Lesser General
** Public License.
**
** 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "msutil.h"
#include "_acism.h"
#include <assert.h>
#include <stdio.h>
#if !defined(_MSC_VER)
# include <unistd.h>
# include <sys/mman.h>
#endif
#ifndef MAP_NOCORE
# define MAP_NOCORE 0
#endif
void
acism_save(FILE *fp, ACISM const*psp)
{
ACISM ps = *psp;
// Overwrite pointers with magic signature.
assert(8 <= sizeof ps.tranv + sizeof ps.hashv);
memcpy(&ps, "ACMischa", 8);
ps.flags &= ~IS_MMAP;
fwrite(&ps, sizeof(ACISM), 1, fp);
fwrite(psp->tranv, p_size(psp), 1, fp);
}
ACISM*
acism_load(FILE *fp)
{
ACISM *psp = calloc(sizeof(ACISM), 1);
if (fread(psp, sizeof(ACISM), 1, fp) == 1
&& !memcmp(psp, "ACMischa", 8)
&& (set_tranv(psp, malloc(p_size(psp))), 1)
&& fread(psp->tranv, p_size(psp), 1, fp)) {
return psp;
}
acism_destroy(psp);
return NULL;
}
#if !defined(_MSC_VER)
//XXX add mmap for VS
ACISM*
acism_mmap(FILE *fp)
{
ACISM *mp = mmap(0, lseek(fileno(fp), 0L, 2), PROT_READ,
MAP_SHARED|MAP_NOCORE, fileno(fp), 0);
if (mp == MAP_FAILED) return NULL;
ACISM *psp = malloc(sizeof*psp);
*psp = *(ACISM*)mp;
psp->flags |= IS_MMAP;
if (memcmp(psp, "ACMischa", 8)) {
acism_destroy(psp);
return NULL;
}
set_tranv(psp, ((char *)mp) + sizeof(ACISM));
return psp;
}
#endif//_MSC_VER
void
acism_destroy(ACISM *psp)
{
if (!psp) return;
#ifdef XXX
if (psp->flags & IS_MMAP)
munmap((char*)psp->tranv - sizeof(ACISM), sizeof(ACISM) + p_size(psp));
else
#endif//XXX
free(psp->tranv);
free(psp);
}
//EOF