Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #4825

Re: Malloc Query

From Uncle Steve <stevet810@gmail.com>
Newsgroups comp.lang.c
Subject Re: Malloc Query
Date 2011-05-26 15:02 -0400
Organization Friends of the Galactic Collective
Message-ID <3f300b882b4511e-c3e99@gmail.com> (permalink)
References (5 earlier) <0.3ac369f64d11bcd06d18.20110519002615BST.87ei3vtwtk.fsf@bsb.me.uk> <98a074c87051012-452fb@gmail.com> <0.bf5e9ff46dfbfc1e78d6.20110519032218BST.87tycrsa3p.fsf@bsb.me.uk> <fb81382c91724d0-17e8d@gmail.com> <4dddef1f$0$38644$4fafbaef@reader1.news.tin.it>

Show all headers | View raw


On Thu, May 26, 2011 at 08:12:59AM +0200, io_x wrote:
> 
> "Uncle Steve" <stevet810@gmail.com> ha scritto nel messaggio
> news:fb81382c91724d0-17e8d@gmail.com...
> > On Thu, May 19, 2011 at 03:22:18AM +0100, Ben Bacarisse wrote:
> > [snip]
> >
> >
> > Ok, here's a quick and dirty hack that measures a highly contrived
> > scenario.  This code first allocates two arenas of different size, and
> > then ping-pongs the allocations between the two arenas for a defined
> > number of iterations.  Then, I do more or less the same thing with
> > malloc.y
> >
> > The preliminary results show that my special-purpose allocator is 2-3
> > times faster than glibc malloc.  Not quite and order-of-magnitude
> > difference in performance (unless you think in base-2), but very
> > acceptable.  In some programs there may be a larger difference in
> > performance becuase of reduceed memory fragmentation or reduced
> > overall memory use.  I do not plan to isolate those factors at this
> > time to measure their effect.
> >
> > Code is compiled with "gcc -O0 -o arena_test arena_test.c -lrt"
> >
> > The test platform here is an Intel Atom netbook running at 1.333GHz,
> > 1G RAM, OpenSuSE 11.4, libc-2.11.3, gcc 4.5.1 20101208.
> >
> > Typical result:
> >
> > [31/22:32] nb:pts/10 stevet ~/stuff/src/libs/tools/test: ./arena_test
> > Arena: Iterations: 100000; elapsed CPU time (msec): 39181
> > Malloc:  Iterations: 100000; elapsed CPU time (msec): 107265
> >
> >
> > Code follows:
> >
> > #include <stdio.h>
> > #include <malloc.h>
> > #include <stdlib.h>
> > #include <time.h>
> > #include <sys/time.h>
> >
> >
> > #define TEST_SIZE    1024 * 1024
> > #define ITERATIONS   100000
> >
> > #define DIFFERENCE   5
> >
> >
> > struct arena_head_s {
> >   int      obj_size;
> >   int      arena_size;
> >   unsigned char *
> >            arena;
> >   int      free;
> >   int      free_list;
> > };
> >
> > typedef struct arena_head_s arena_head;
> >
> >
> > #define arena_obj_addr(x, n) ((void *) &x->arena[n * x->obj_size])
> >
> >
> > arena_qa(arena_head *x)
> > {
> >   int n;
> >
> >   n = x->free_list;
> >
> >   if(n != -1) {
> >      x->free_list = *((int *) arena_obj_addr(x, n));
> >      x->free--;
> >   }
> >
> >   return(n);
> > }
> >
> > void arena_free(arena_head *p, int n)
> > {
> >   *((int *) arena_obj_addr(p, n)) = p->free_list;
> >   p->free_list = n;
> >   p->free++;
> >
> >   return;
> > }
> >
> > arena_head * arena_creat(size_t s, int n)
> > {
> >   arena_head * h;
> >   int i;
> >
> >   h = malloc(sizeof(arena_head));
> >   if(h == NULL) {
> >      perror("malloc()");
> >      exit(1);
> >   }
> >
> >   h->obj_size = s;
> >   h->arena_size = n;
> >   h->free = n;
> >
> >   h->arena = malloc(s * n);
> >   if(h->arena == NULL) {
> >      perror("malloc()");
> >      exit(1);
> >   }
> >
> >   h->free_list = 0;
> >
> >   for(i = 0; i < n; i++)
> >      *((int *) arena_obj_addr(h, i)) = i + 1;
> >
> >   *((int *) arena_obj_addr(h, i)) = -1;
> 
> this above instruction seems to write in a place where has not to write
> Is it possible the space is for n elements but here it is written the
> element n+1 [i=n not in 0..n-1]?

All that statement does is simulate defining the target arena object
as a union of int, and whatever the caller plans to store in each
object slot.  It's actually a more convenient solution because in the
case where you do use a union, the code in the loop above would have
to look something like this.


for(i =0; i < (n -1); i++)
   h->arena[i].un.freelist_next = i + 1;

h->arena[i].un.freelist_next = -1;

And accessing whatever it is that is actually being stored there would
have to be similar to *arena_obj_addr(h, i) = whatever.  As the free
list management is internal to the alocator, the application should
not care what is stored in the free slots of the 'array'.

> i'm not much smart, i not understand well all this code
> but it seems to me the bigger error it is all can overflow and
> too much confidence all goes well to the HLL

There was an off-by-one error in the above code from a transcription
error, which was resolved in a subsequent message.  Otherwise, an
out-of-bounds access is the fault of the application programmer.
Caveat emptor.

> how is good assembly :)
> i have the difect i see something good each little instruction at time
> [too much near and too much far]
> 
> >   return(h);
> > }

As others have said, the requirements of kernel programming are
different from conventional application logic.  But this code is not
part of an operating system, or anything even remotely similar to an
operating system.  It is a kernel only in a limited and specialized
sense.



Regards,

Uncle Steve

-- 
Should a professional politician be charged with molestation if he
kisses babies while attending political rallies?

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Malloc Query Pushpa <pushpa@nospam.invalid> - 2011-05-17 19:39 +0000
  Re: Malloc Query blp@cs.stanford.edu (Ben Pfaff) - 2011-05-17 12:55 -0700
    Re: Malloc Query "BartC" <bc@freeuk.com> - 2011-05-18 12:05 +0100
      Re: Malloc Query Pushpa <pushpa@nospam.invalid> - 2011-05-18 17:11 +0000
        Re: Malloc Query "BartC" <bc@freeuk.com> - 2011-05-18 22:42 +0100
        Re: Malloc Query Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-18 21:10 -0400
      Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-18 19:34 +0100
        Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-18 17:10 -0400
          Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-19 00:26 +0100
            Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-18 21:25 -0400
              Re: Malloc Query James Kuyper <jameskuyper@verizon.net> - 2011-05-18 21:38 -0400
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-19 14:11 -0400
                Re: Malloc Query James Kuyper <jameskuyper@verizon.net> - 2011-05-19 22:55 -0400
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-20 12:14 -0400
              Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-19 03:22 +0100
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-19 14:20 -0400
                Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-19 23:51 +0100
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-19 20:01 -0400
                Re: Malloc Query luser- -droog <mijoryx@yahoo.com> - 2011-05-22 02:49 -0700
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-22 10:46 -0400
                Re: Malloc Query Shao Miller <sha0.miller@gmail.com> - 2011-05-22 12:37 -0500
                Re: Malloc Query Shao Miller <sha0.miller@gmail.com> - 2011-05-22 13:02 -0500
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-22 14:12 -0400
                Re: Malloc Query Shao Miller <sha0.miller@gmail.com> - 2011-05-23 03:06 -0500
                Re: Malloc Query luser- -droog <mijoryx@yahoo.com> - 2011-05-23 00:42 -0700
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-23 11:10 -0400
                Re: Malloc Query Keith Thompson <kst-u@mib.org> - 2011-05-23 09:06 -0700
                Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-22 23:18 +0100
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-22 19:18 -0400
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-23 11:22 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-23 01:57 -0400
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-23 19:24 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-23 14:20 -0400
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-24 07:41 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-23 16:24 -0400
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-24 09:20 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-23 18:05 -0400
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-24 10:37 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-23 19:34 -0400
                Re: Malloc Query "robertwessel2@yahoo.com" <robertwessel2@yahoo.com> - 2011-05-23 17:48 -0700
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-24 12:56 +1200
                Re: Malloc Query James Kuyper <jameskuyper@verizon.net> - 2011-05-24 07:19 -0400
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-24 11:32 -0400
                Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-23 01:04 +0100
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-23 02:36 -0400
                Re: Malloc Query luser- -droog <mijoryx@yahoo.com> - 2011-05-23 00:39 -0700
                Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-23 14:44 +0100
                Re: Malloc Query Seebs <usenet-nospam@seebs.net> - 2011-05-23 15:16 +0000
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-23 11:41 -0400
                Re: Malloc Query David Thompson <dave.thompson2@verizon.net> - 2011-06-01 16:33 -0400
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-19 18:34 -0400
                Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-20 02:36 +0100
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-20 12:26 -0400
                Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-20 23:32 +0100
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-20 21:17 -0400
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-21 14:30 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-21 11:13 -0400
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-22 08:50 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-21 20:03 -0400
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-22 13:23 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-22 10:01 -0400
                Re: Malloc Query "io_x" <a@b.c.invalid> - 2011-05-26 08:12 +0200
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-26 19:16 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-26 15:03 -0400
                Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-27 09:16 +1200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-26 17:55 -0400
                Re: Malloc Query "io_x" <a@b.c.invalid> - 2011-05-27 05:47 +0200
                Re: Malloc Query Uncle Steve <stevet810@gmail.com> - 2011-05-26 15:02 -0400
        Re: Malloc Query "BartC" <bc@freeuk.com> - 2011-05-18 22:31 +0100
          Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-18 23:37 +0100
            Re: Malloc Query "BartC" <bc@freeuk.com> - 2011-05-19 01:50 +0100
              Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-19 02:50 +0100
                Re: Malloc Query "BartC" <bc@freeuk.com> - 2011-05-19 12:14 +0100
                Re: Malloc Query Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-19 15:10 +0100
                Re: Malloc Query "BartC" <bc@freeuk.com> - 2011-05-19 15:48 +0100
      Re: Malloc Query cri@tiac.net (Richard Harter) - 2011-05-18 19:31 +0000
      Re: Malloc Query Ike Naar <ike@iceland.freeshell.org> - 2011-05-18 21:43 +0000
        Re: Malloc Query "BartC" <bc@freeuk.com> - 2011-05-18 22:58 +0100
  Re: Malloc Query Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-17 14:01 -0600
  Re: Malloc Query Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-17 13:21 -0700
  Re: Malloc Query BGB <cr88192@hotmail.com> - 2011-05-17 14:08 -0700
    Re: Malloc Query Michael Press <rubrum@pacbell.net> - 2011-05-17 19:49 -0700
  Re: Malloc Query Lauri Alanko <la@iki.fi> - 2011-05-18 00:50 +0000
  Re: Malloc Query Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-17 21:21 -0400
    Re: Malloc Query "io_x" <a@b.c.invalid> - 2011-05-18 19:31 +0200
    Re: Malloc Query Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-05-18 21:36 +0000
      Re: Malloc Query Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-18 21:26 -0400
    Re: Malloc Query Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-19 02:07 -0700
      Re: Malloc Query Ian Collins <ian-news@hotmail.com> - 2011-05-19 21:13 +1200
  Re: Malloc Query Nobody <nobody@nowhere.com> - 2011-05-18 07:08 +0100
  Re: Malloc Query David Resnick <lndresnick@gmail.com> - 2011-05-19 07:51 -0700
    Re: Malloc Query Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-19 09:12 -0600

csiph-web