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"
23 int ewah_serialize_native(struct ewah_bitmap
*self
, int fd
)
26 size_t to_write
= self
->buffer_size
* 8;
28 /* 32 bit -- bit size for the map */
29 write32
= (uint32_t)self
->bit_size
;
30 if (write(fd
, &write32
, 4) != 4)
33 /** 32 bit -- number of compressed 64-bit words */
34 write32
= (uint32_t)self
->buffer_size
;
35 if (write(fd
, &write32
, 4) != 4)
38 if (write(fd
, self
->buffer
, to_write
) != to_write
)
41 /** 32 bit -- position for the RLW */
42 write32
= self
->rlw
- self
->buffer
;
43 if (write(fd
, &write32
, 4) != 4)
46 return (3 * 4) + to_write
;
49 int ewah_serialize_to(struct ewah_bitmap
*self
,
50 int (*write_fun
)(void *, const void *, size_t),
55 const size_t words_per_dump
= sizeof(dump
) / sizeof(eword_t
);
56 uint32_t bitsize
, word_count
, rlw_pos
;
58 const eword_t
*buffer
;
61 /* 32 bit -- bit size for the map */
62 bitsize
= htonl((uint32_t)self
->bit_size
);
63 if (write_fun(data
, &bitsize
, 4) != 4)
66 /** 32 bit -- number of compressed 64-bit words */
67 word_count
= htonl((uint32_t)self
->buffer_size
);
68 if (write_fun(data
, &word_count
, 4) != 4)
71 /** 64 bit x N -- compressed words */
72 buffer
= self
->buffer
;
73 words_left
= self
->buffer_size
;
75 while (words_left
>= words_per_dump
) {
76 for (i
= 0; i
< words_per_dump
; ++i
, ++buffer
)
77 dump
[i
] = htonll(*buffer
);
79 if (write_fun(data
, dump
, sizeof(dump
)) != sizeof(dump
))
82 words_left
-= words_per_dump
;
86 for (i
= 0; i
< words_left
; ++i
, ++buffer
)
87 dump
[i
] = htonll(*buffer
);
89 if (write_fun(data
, dump
, words_left
* 8) != words_left
* 8)
93 /** 32 bit -- position for the RLW */
94 rlw_pos
= (uint8_t*)self
->rlw
- (uint8_t *)self
->buffer
;
95 rlw_pos
= htonl(rlw_pos
/ sizeof(eword_t
));
97 if (write_fun(data
, &rlw_pos
, 4) != 4)
100 return (3 * 4) + (self
->buffer_size
* 8);
103 static int write_helper(void *fd
, const void *buf
, size_t len
)
105 return write((intptr_t)fd
, buf
, len
);
108 int ewah_serialize(struct ewah_bitmap
*self
, int fd
)
110 return ewah_serialize_to(self
, write_helper
, (void *)(intptr_t)fd
);
113 int ewah_read_mmap(struct ewah_bitmap
*self
, const void *map
, size_t len
)
115 const uint8_t *ptr
= map
;
118 self
->bit_size
= get_be32(ptr
);
119 ptr
+= sizeof(uint32_t);
121 self
->buffer_size
= self
->alloc_size
= get_be32(ptr
);
122 ptr
+= sizeof(uint32_t);
124 self
->buffer
= ewah_realloc(self
->buffer
,
125 self
->alloc_size
* sizeof(eword_t
));
131 * Copy the raw data for the bitmap as a whole chunk;
132 * if we're in a little-endian platform, we'll perform
133 * the endianness conversion in a separate pass to ensure
134 * we're loading 8-byte aligned words.
136 memcpy(self
->buffer
, ptr
, self
->buffer_size
* sizeof(uint64_t));
137 ptr
+= self
->buffer_size
* sizeof(uint64_t);
139 for (i
= 0; i
< self
->buffer_size
; ++i
)
140 self
->buffer
[i
] = ntohll(self
->buffer
[i
]);
142 self
->rlw
= self
->buffer
+ get_be32(ptr
);
144 return (3 * 4) + (self
->buffer_size
* 8);
147 int ewah_deserialize(struct ewah_bitmap
*self
, int fd
)
151 const size_t words_per_dump
= sizeof(dump
) / sizeof(eword_t
);
152 uint32_t bitsize
, word_count
, rlw_pos
;
154 eword_t
*buffer
= NULL
;
159 /* 32 bit -- bit size for the map */
160 if (read(fd
, &bitsize
, 4) != 4)
163 self
->bit_size
= (size_t)ntohl(bitsize
);
165 /** 32 bit -- number of compressed 64-bit words */
166 if (read(fd
, &word_count
, 4) != 4)
169 self
->buffer_size
= self
->alloc_size
= (size_t)ntohl(word_count
);
170 self
->buffer
= ewah_realloc(self
->buffer
,
171 self
->alloc_size
* sizeof(eword_t
));
176 /** 64 bit x N -- compressed words */
177 buffer
= self
->buffer
;
178 words_left
= self
->buffer_size
;
180 while (words_left
>= words_per_dump
) {
181 if (read(fd
, dump
, sizeof(dump
)) != sizeof(dump
))
184 for (i
= 0; i
< words_per_dump
; ++i
, ++buffer
)
185 *buffer
= ntohll(dump
[i
]);
187 words_left
-= words_per_dump
;
191 if (read(fd
, dump
, words_left
* 8) != words_left
* 8)
194 for (i
= 0; i
< words_left
; ++i
, ++buffer
)
195 *buffer
= ntohll(dump
[i
]);
198 /** 32 bit -- position for the RLW */
199 if (read(fd
, &rlw_pos
, 4) != 4)
202 self
->rlw
= self
->buffer
+ ntohl(rlw_pos
);