From 03d20062b4050bd612b5d2286f1a9a10fba406ed Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Fri, 29 Apr 2011 11:43:18 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=83=D0=BB=20=D0=BF=D0=B0=D0=BC=D1=8F?= =?UTF-8?q?=D1=82=D0=B8=20=D1=81=D0=B2=D0=BE=D0=B9=20=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D1=8E,=20=D0=BF=D0=BE=D0=BA=D0=B0=20=D1=87=D1=82=D0=BE?= =?UTF-8?q?=20=D1=83=D0=B4=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=BE=D0=B1?= =?UTF-8?q?=D0=BE=D0=B3=D0=BD=D0=B0=D1=82=D1=8C=20=D0=B2=205-6=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=20c++:vector.push=5Fback=20=D0=B8=20=D0=B2=20~10?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B7=20malloc,=20=D0=BD=D1=83=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B5=D1=89=D1=91=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=202=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BD=D1=8F=D1=82=D1=8B=D1=85=20=D0=B8=20=D0=BD?= =?UTF-8?q?=D0=B5=D0=B7=D0=B0=D0=BD=D1=8F=D1=82=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c/malloc_speed_test/pool1.c | 64 ++++++++++++++++++++++++++++++ c/malloc_speed_test/run.sh | 7 +++- c/malloc_speed_test/vector_add.cpp | 2 +- c/mstats_ex/mstats_ex.c | 13 ++++++ 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 c/malloc_speed_test/pool1.c create mode 100644 c/mstats_ex/mstats_ex.c 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; +} +