Fix non-swap configurations
[hed.git] / libhed / swap.h
bloba52b4a4d9fd81cf58aab1c9e09469913ed00b9d4
1 /* Swap file handling */
3 /* This is a libhed PRIVATE file.
4 * Do not include outside of libhed. Do not install on the system.
5 */
7 /*
8 * hed - Hexadecimal editor
9 * Copyright (C) 2011 Petr Tesarik <petr@tesarici.cz>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifndef LIBHED__SWAP_H
26 #define LIBHED__SWAP_H
28 #include "config.h"
29 #include "types.h"
31 /* Prevent polluting linker namespace with internal symbols */
32 #include "private.h"
33 #define swp_cpin internal_name(swp_cpin)
34 #define swp_done internal_name(swp_done)
35 #define swp_free internal_name(swp_free)
36 #define swp_init_read internal_name(swp_init_read)
37 #define swp_init_write internal_name(swp_init_write)
38 #define swp_malloc internal_name(swp_malloc)
39 #define swp_private internal_name(swp_private)
40 #define swp_shrink internal_name(swp_shrink)
41 #define swp_write internal_name(swp_write)
42 #define swp_zalloc internal_name(swp_zalloc)
44 #ifdef HED_CONFIG_SWAP
46 #define SWAP_SIGNATURE "HEDSWAP\n"
47 #define SIG_LEN (sizeof(SWAP_SIGNATURE) - 1)
48 #define SWAP_VERSION 2
50 struct swp_header {
51 char signature[SIG_LEN]; /* = "HEDSWAP\n" */
52 short version; /* Swap file version */
53 size_t offsize;
54 void *addr; /* Address of the file header itself */
57 struct swp_alloc;
59 struct swp_file {
60 struct swp_header *hdr;
62 /* Swap implementation private fields */
63 int fd;
64 hed_uoff_t size;
66 #ifdef HED_CONFIG_SWAP_MMAP
67 int mmap_prot;
68 int mmap_flags;
69 #endif
71 struct list_head freeblocks;
72 struct list_head usedblocks;
73 void ***maps;
74 unsigned nmaps;
77 struct swp_file *swp_init_write(const char *name);
78 struct swp_file *swp_init_read(const char *name);
79 int swp_done(struct swp_file *swp);
80 void *swp_malloc(struct swp_file *swp, size_t size);
81 void *swp_zalloc(struct swp_file *swp, size_t size);
82 void *swp_shrink(struct swp_file *swp, void *ptr, size_t newsize);
83 void swp_free(struct swp_file *swp, void *ptr);
85 int swp_write(struct swp_file *swp);
86 size_t swp_cpin(struct swp_file *swp, void *buf, void *ptr, size_t len);
88 static inline void *
89 swp_private(struct swp_file *swp)
91 return swp->hdr + 1;
94 #else /* HED_CONFIG_SWAP */
96 #include <stdlib.h>
98 struct swp_file;
100 static inline void *
101 swp_malloc(struct swp_file *swp, size_t size)
103 return malloc(size);
106 static inline void *
107 swp_zalloc(struct swp_file *swp, size_t size)
109 return calloc(1, size);
112 static inline void *
113 swp_shrink(struct swp_file *swp, void *ptr, size_t newsize)
115 return realloc(ptr, newsize);
118 static inline void
119 swp_free(struct swp_file *swp, void *ptr)
121 free(ptr);
124 #endif /* HED_CONFIG_SWAP */
126 #endif /* LIBHED__SWAP_H */