i18n: merge: change command option help to lowercase
[git/debian.git] / ewah / ewah_io.c
blob61f6a43579f5e3fc98e562fe6f0c6d8c118e0ded
1 /**
2 * Copyright 2013, GitHub, Inc
3 * Copyright 2009-2013, Daniel Lemire, Cliff Moon,
4 * David McIntosh, Robert Becho, Google Inc. and Veronika Zenz
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "git-compat-util.h"
21 #include "ewok.h"
22 #include "strbuf.h"
24 int ewah_serialize_native(struct ewah_bitmap *self, int fd)
26 uint32_t write32;
27 size_t to_write = self->buffer_size * 8;
29 /* 32 bit -- bit size for the map */
30 write32 = (uint32_t)self->bit_size;
31 if (write(fd, &write32, 4) != 4)
32 return -1;
34 /** 32 bit -- number of compressed 64-bit words */
35 write32 = (uint32_t)self->buffer_size;
36 if (write(fd, &write32, 4) != 4)
37 return -1;
39 if (write(fd, self->buffer, to_write) != to_write)
40 return -1;
42 /** 32 bit -- position for the RLW */
43 write32 = self->rlw - self->buffer;
44 if (write(fd, &write32, 4) != 4)
45 return -1;
47 return (3 * 4) + to_write;
50 int ewah_serialize_to(struct ewah_bitmap *self,
51 int (*write_fun)(void *, const void *, size_t),
52 void *data)
54 size_t i;
55 eword_t dump[2048];
56 const size_t words_per_dump = sizeof(dump) / sizeof(eword_t);
57 uint32_t bitsize, word_count, rlw_pos;
59 const eword_t *buffer;
60 size_t words_left;
62 /* 32 bit -- bit size for the map */
63 bitsize = htonl((uint32_t)self->bit_size);
64 if (write_fun(data, &bitsize, 4) != 4)
65 return -1;
67 /** 32 bit -- number of compressed 64-bit words */
68 word_count = htonl((uint32_t)self->buffer_size);
69 if (write_fun(data, &word_count, 4) != 4)
70 return -1;
72 /** 64 bit x N -- compressed words */
73 buffer = self->buffer;
74 words_left = self->buffer_size;
76 while (words_left >= words_per_dump) {
77 for (i = 0; i < words_per_dump; ++i, ++buffer)
78 dump[i] = htonll(*buffer);
80 if (write_fun(data, dump, sizeof(dump)) != sizeof(dump))
81 return -1;
83 words_left -= words_per_dump;
86 if (words_left) {
87 for (i = 0; i < words_left; ++i, ++buffer)
88 dump[i] = htonll(*buffer);
90 if (write_fun(data, dump, words_left * 8) != words_left * 8)
91 return -1;
94 /** 32 bit -- position for the RLW */
95 rlw_pos = (uint8_t*)self->rlw - (uint8_t *)self->buffer;
96 rlw_pos = htonl(rlw_pos / sizeof(eword_t));
98 if (write_fun(data, &rlw_pos, 4) != 4)
99 return -1;
101 return (3 * 4) + (self->buffer_size * 8);
104 static int write_helper(void *fd, const void *buf, size_t len)
106 return write((intptr_t)fd, buf, len);
109 int ewah_serialize(struct ewah_bitmap *self, int fd)
111 return ewah_serialize_to(self, write_helper, (void *)(intptr_t)fd);
114 static int write_strbuf(void *user_data, const void *data, size_t len)
116 struct strbuf *sb = user_data;
117 strbuf_add(sb, data, len);
118 return len;
121 int ewah_serialize_strbuf(struct ewah_bitmap *self, struct strbuf *sb)
123 return ewah_serialize_to(self, write_strbuf, sb);
126 int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
128 const uint8_t *ptr = map;
129 size_t i;
131 self->bit_size = get_be32(ptr);
132 ptr += sizeof(uint32_t);
134 self->buffer_size = self->alloc_size = get_be32(ptr);
135 ptr += sizeof(uint32_t);
137 REALLOC_ARRAY(self->buffer, self->alloc_size);
140 * Copy the raw data for the bitmap as a whole chunk;
141 * if we're in a little-endian platform, we'll perform
142 * the endianness conversion in a separate pass to ensure
143 * we're loading 8-byte aligned words.
145 memcpy(self->buffer, ptr, self->buffer_size * sizeof(uint64_t));
146 ptr += self->buffer_size * sizeof(uint64_t);
148 for (i = 0; i < self->buffer_size; ++i)
149 self->buffer[i] = ntohll(self->buffer[i]);
151 self->rlw = self->buffer + get_be32(ptr);
153 return (3 * 4) + (self->buffer_size * 8);
156 int ewah_deserialize(struct ewah_bitmap *self, int fd)
158 size_t i;
159 eword_t dump[2048];
160 const size_t words_per_dump = sizeof(dump) / sizeof(eword_t);
161 uint32_t bitsize, word_count, rlw_pos;
163 eword_t *buffer = NULL;
164 size_t words_left;
166 ewah_clear(self);
168 /* 32 bit -- bit size for the map */
169 if (read(fd, &bitsize, 4) != 4)
170 return -1;
172 self->bit_size = (size_t)ntohl(bitsize);
174 /** 32 bit -- number of compressed 64-bit words */
175 if (read(fd, &word_count, 4) != 4)
176 return -1;
178 self->buffer_size = self->alloc_size = (size_t)ntohl(word_count);
179 REALLOC_ARRAY(self->buffer, self->alloc_size);
181 /** 64 bit x N -- compressed words */
182 buffer = self->buffer;
183 words_left = self->buffer_size;
185 while (words_left >= words_per_dump) {
186 if (read(fd, dump, sizeof(dump)) != sizeof(dump))
187 return -1;
189 for (i = 0; i < words_per_dump; ++i, ++buffer)
190 *buffer = ntohll(dump[i]);
192 words_left -= words_per_dump;
195 if (words_left) {
196 if (read(fd, dump, words_left * 8) != words_left * 8)
197 return -1;
199 for (i = 0; i < words_left; ++i, ++buffer)
200 *buffer = ntohll(dump[i]);
203 /** 32 bit -- position for the RLW */
204 if (read(fd, &rlw_pos, 4) != 4)
205 return -1;
207 self->rlw = self->buffer + ntohl(rlw_pos);
208 return 0;