Browse Source
This reverts commit 4709455db3
(Add
memory pool library, 2010-08-09). svn-fe uses strbufs to avoid memory
allocation overhead nowadays.
Signed-off-by: David Barr <david.barr@cordelta.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
maint
![david.barr@cordelta.com](/assets/img/avatar_default.png)
![Jonathan Nieder](/assets/img/avatar_default.png)
5 changed files with 1 additions and 260 deletions
@ -1,79 +0,0 @@
@@ -1,79 +0,0 @@
|
||||
#!/bin/sh |
||||
|
||||
test_description='check infrastructure for svn importer' |
||||
|
||||
. ./test-lib.sh |
||||
uint32_max=4294967295 |
||||
|
||||
test_expect_success 'obj pool: store data' ' |
||||
cat <<-\EOF >expected && |
||||
0 |
||||
1 |
||||
EOF |
||||
|
||||
test-obj-pool <<-\EOF >actual && |
||||
alloc one 16 |
||||
set one 13 |
||||
test one 13 |
||||
reset one |
||||
EOF |
||||
test_cmp expected actual |
||||
' |
||||
|
||||
test_expect_success 'obj pool: NULL is offset ~0' ' |
||||
echo "$uint32_max" >expected && |
||||
echo null one | test-obj-pool >actual && |
||||
test_cmp expected actual |
||||
' |
||||
|
||||
test_expect_success 'obj pool: out-of-bounds access' ' |
||||
cat <<-EOF >expected && |
||||
0 |
||||
0 |
||||
$uint32_max |
||||
$uint32_max |
||||
16 |
||||
20 |
||||
$uint32_max |
||||
EOF |
||||
|
||||
test-obj-pool <<-\EOF >actual && |
||||
alloc one 16 |
||||
alloc two 16 |
||||
offset one 20 |
||||
offset two 20 |
||||
alloc one 5 |
||||
offset one 20 |
||||
free one 1 |
||||
offset one 20 |
||||
reset one |
||||
reset two |
||||
EOF |
||||
test_cmp expected actual |
||||
' |
||||
|
||||
test_expect_success 'obj pool: high-water mark' ' |
||||
cat <<-\EOF >expected && |
||||
0 |
||||
0 |
||||
10 |
||||
20 |
||||
20 |
||||
20 |
||||
EOF |
||||
|
||||
test-obj-pool <<-\EOF >actual && |
||||
alloc one 10 |
||||
committed one |
||||
alloc one 10 |
||||
commit one |
||||
committed one |
||||
alloc one 10 |
||||
free one 20 |
||||
committed one |
||||
reset one |
||||
EOF |
||||
test_cmp expected actual |
||||
' |
||||
|
||||
test_done |
@ -1,116 +0,0 @@
@@ -1,116 +0,0 @@
|
||||
/* |
||||
* test-obj-pool.c: code to exercise the svn importer's object pool |
||||
*/ |
||||
|
||||
#include "cache.h" |
||||
#include "vcs-svn/obj_pool.h" |
||||
|
||||
enum pool { POOL_ONE, POOL_TWO }; |
||||
obj_pool_gen(one, int, 1) |
||||
obj_pool_gen(two, int, 4096) |
||||
|
||||
static uint32_t strtouint32(const char *s) |
||||
{ |
||||
char *end; |
||||
uintmax_t n = strtoumax(s, &end, 10); |
||||
if (*s == '\0' || (*end != '\n' && *end != '\0')) |
||||
die("invalid offset: %s", s); |
||||
return (uint32_t) n; |
||||
} |
||||
|
||||
static void handle_command(const char *command, enum pool pool, const char *arg) |
||||
{ |
||||
switch (*command) { |
||||
case 'a': |
||||
if (!prefixcmp(command, "alloc ")) { |
||||
uint32_t n = strtouint32(arg); |
||||
printf("%"PRIu32"\n", |
||||
pool == POOL_ONE ? |
||||
one_alloc(n) : two_alloc(n)); |
||||
return; |
||||
} |
||||
case 'c': |
||||
if (!prefixcmp(command, "commit ")) { |
||||
pool == POOL_ONE ? one_commit() : two_commit(); |
||||
return; |
||||
} |
||||
if (!prefixcmp(command, "committed ")) { |
||||
printf("%"PRIu32"\n", |
||||
pool == POOL_ONE ? |
||||
one_pool.committed : two_pool.committed); |
||||
return; |
||||
} |
||||
case 'f': |
||||
if (!prefixcmp(command, "free ")) { |
||||
uint32_t n = strtouint32(arg); |
||||
pool == POOL_ONE ? one_free(n) : two_free(n); |
||||
return; |
||||
} |
||||
case 'n': |
||||
if (!prefixcmp(command, "null ")) { |
||||
printf("%"PRIu32"\n", |
||||
pool == POOL_ONE ? |
||||
one_offset(NULL) : two_offset(NULL)); |
||||
return; |
||||
} |
||||
case 'o': |
||||
if (!prefixcmp(command, "offset ")) { |
||||
uint32_t n = strtouint32(arg); |
||||
printf("%"PRIu32"\n", |
||||
pool == POOL_ONE ? |
||||
one_offset(one_pointer(n)) : |
||||
two_offset(two_pointer(n))); |
||||
return; |
||||
} |
||||
case 'r': |
||||
if (!prefixcmp(command, "reset ")) { |
||||
pool == POOL_ONE ? one_reset() : two_reset(); |
||||
return; |
||||
} |
||||
case 's': |
||||
if (!prefixcmp(command, "set ")) { |
||||
uint32_t n = strtouint32(arg); |
||||
if (pool == POOL_ONE) |
||||
*one_pointer(n) = 1; |
||||
else |
||||
*two_pointer(n) = 1; |
||||
return; |
||||
} |
||||
case 't': |
||||
if (!prefixcmp(command, "test ")) { |
||||
uint32_t n = strtouint32(arg); |
||||
printf("%d\n", pool == POOL_ONE ? |
||||
*one_pointer(n) : *two_pointer(n)); |
||||
return; |
||||
} |
||||
default: |
||||
die("unrecognized command: %s", command); |
||||
} |
||||
} |
||||
|
||||
static void handle_line(const char *line) |
||||
{ |
||||
const char *arg = strchr(line, ' '); |
||||
enum pool pool; |
||||
|
||||
if (arg && !prefixcmp(arg + 1, "one")) |
||||
pool = POOL_ONE; |
||||
else if (arg && !prefixcmp(arg + 1, "two")) |
||||
pool = POOL_TWO; |
||||
else |
||||
die("no pool specified: %s", line); |
||||
|
||||
handle_command(line, pool, arg + strlen("one ")); |
||||
} |
||||
|
||||
int main(int argc, char *argv[]) |
||||
{ |
||||
struct strbuf sb = STRBUF_INIT; |
||||
if (argc != 1) |
||||
usage("test-obj-str < script"); |
||||
|
||||
while (strbuf_getline(&sb, stdin, '\n') != EOF) |
||||
handle_line(sb.buf); |
||||
strbuf_release(&sb); |
||||
return 0; |
||||
} |
@ -1,61 +0,0 @@
@@ -1,61 +0,0 @@
|
||||
/* |
||||
* Licensed under a two-clause BSD-style license. |
||||
* See LICENSE for details. |
||||
*/ |
||||
|
||||
#ifndef OBJ_POOL_H_ |
||||
#define OBJ_POOL_H_ |
||||
|
||||
#include "git-compat-util.h" |
||||
|
||||
#define MAYBE_UNUSED __attribute__((__unused__)) |
||||
|
||||
#define obj_pool_gen(pre, obj_t, initial_capacity) \ |
||||
static struct { \ |
||||
uint32_t committed; \ |
||||
uint32_t size; \ |
||||
uint32_t capacity; \ |
||||
obj_t *base; \ |
||||
} pre##_pool = {0, 0, 0, NULL}; \ |
||||
static MAYBE_UNUSED uint32_t pre##_alloc(uint32_t count) \ |
||||
{ \ |
||||
uint32_t offset; \ |
||||
if (pre##_pool.size + count > pre##_pool.capacity) { \ |
||||
while (pre##_pool.size + count > pre##_pool.capacity) \ |
||||
if (pre##_pool.capacity) \ |
||||
pre##_pool.capacity *= 2; \ |
||||
else \ |
||||
pre##_pool.capacity = initial_capacity; \ |
||||
pre##_pool.base = realloc(pre##_pool.base, \ |
||||
pre##_pool.capacity * sizeof(obj_t)); \ |
||||
} \ |
||||
offset = pre##_pool.size; \ |
||||
pre##_pool.size += count; \ |
||||
return offset; \ |
||||
} \ |
||||
static MAYBE_UNUSED void pre##_free(uint32_t count) \ |
||||
{ \ |
||||
pre##_pool.size -= count; \ |
||||
} \ |
||||
static MAYBE_UNUSED uint32_t pre##_offset(obj_t *obj) \ |
||||
{ \ |
||||
return obj == NULL ? ~0 : obj - pre##_pool.base; \ |
||||
} \ |
||||
static MAYBE_UNUSED obj_t *pre##_pointer(uint32_t offset) \ |
||||
{ \ |
||||
return offset >= pre##_pool.size ? NULL : &pre##_pool.base[offset]; \ |
||||
} \ |
||||
static MAYBE_UNUSED void pre##_commit(void) \ |
||||
{ \ |
||||
pre##_pool.committed = pre##_pool.size; \ |
||||
} \ |
||||
static MAYBE_UNUSED void pre##_reset(void) \ |
||||
{ \ |
||||
free(pre##_pool.base); \ |
||||
pre##_pool.base = NULL; \ |
||||
pre##_pool.size = 0; \ |
||||
pre##_pool.capacity = 0; \ |
||||
pre##_pool.committed = 0; \ |
||||
} |
||||
|
||||
#endif |
Loading…
Reference in new issue