diff --git a/c/malloc_speed_test/pool1.c b/c/malloc_speed_test/pool1.c new file mode 100644 index 0000000..2e4ecba --- /dev/null +++ b/c/malloc_speed_test/pool1.c @@ -0,0 +1,64 @@ +#include + +#define MAX_I 0x00ffffff + +#define bool char +#define TRUE 1 +#define FALSE 0 + +struct object_s { + char val; +}; + +struct block_s { + bool busy; + struct object_s obj; +}; + +struct block_s pool[MAX_I]; + +void init() +{ + size_t i; + + for (i = 0; i < MAX_I; i++) + pool[i].busy = TRUE; +} + +#define MAX_B (pool + MAX_I * sizeof(struct block_s)) + +inline void *alloc() +{ + static struct block_s *b = pool; + struct block_s *old_b = b; + + for ( ; b < MAX_B; b++) + if (!b->busy) { + b->busy = TRUE; + return &b->obj; + } + + for (b = pool; b < old_b; b++) + if (!b->busy) { + b->busy = TRUE; + return &b->obj; + } + + return NULL; +} + +inline void free(void *p) +{ + bool *b = (bool *)(p - sizeof(bool)); + *b = FALSE; +} + +int main() +{ + unsigned long long i; + + for (i = 0; i < MAX_I; i++) + alloc(); + + return 0; +} diff --git a/c/malloc_speed_test/run.sh b/c/malloc_speed_test/run.sh index 1f7b393..788c53b 100755 --- a/c/malloc_speed_test/run.sh +++ b/c/malloc_speed_test/run.sh @@ -1,7 +1,10 @@ #!/bin/bash echo "malloc_speed_test.c test:" -cc -O2 malloc_speed_test.c -o malloc_speed_test && time ./malloc_speed_test +cc -O2 malloc_speed_test.c -o malloc_speed_test && time ./malloc_speed_test echo -e "\n\nvector_add.cpp test:" -c++ -O2 vector_add.cpp -o vector_add && time ./vector_add +c++ -O2 vector_add.cpp -o vector_add && time ./vector_add + +echo "pool1.c test:" +cc -O2 pool1.c -o pool1 && time ./pool1 diff --git a/c/malloc_speed_test/vector_add.cpp b/c/malloc_speed_test/vector_add.cpp index e87affe..d852cbe 100644 --- a/c/malloc_speed_test/vector_add.cpp +++ b/c/malloc_speed_test/vector_add.cpp @@ -2,7 +2,7 @@ using namespace std; -const unsigned long long MAX_I = 0x00ffffff; +const unsigned long long MAX_I = 0x0fffffff; int main() { diff --git a/c/mstats_ex/mstats_ex.c b/c/mstats_ex/mstats_ex.c new file mode 100644 index 0000000..8c135d5 --- /dev/null +++ b/c/mstats_ex/mstats_ex.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + struct mstats ms; + + ms = mstats(); + + return EXIT_SUCCESS; +} +