The HyperNews Linux KHG Discussion Pages

## question about alloc_skb in core/net/skbuff.c

  Forum: Network Buffers And Memory Management
  Keywords: alloc_skb kmalloc
  Date: Sat, 27 Dec 1997 07:08:21 GMT
  From: Radu Duta <rduta@usa.net>

I was looking through alloc_skb, and ran across this:
      bptr=(unsigned char *)kmalloc(size,priority);
      skb=(struct sk_buff *)(bptr+size)-1;
      skb->count = 1;   /* only one reference to this */
      skb->data_skb = NULL; /* and we're our own data skb */
There where some comments as well:
      /*
       *  Now we play a little game with the caches. Linux kmalloc is
       *  a bit cache dumb, in fact its just about maximally non
       *  optimal for typical kernel buffers. We actually run faster
       *  by doing the following. Which is to deliberately put the
       *  skb at the _end_ not the start of the memory block.
       */
which don't really make much sense to me, but that's ok. I'm confused by the pointer arithmatic. In the second line after you are given a pointer to a block of memory you are incrementing it by it's size - 1 essentially now it poits to the last byte in the allocated memory, right? so then you do a "skb->count = 1;", which should break everything since you are addressing memory outside of the allocated block, right? Obviously not since it works, but I'm very confused. I thought that skb->count would ADD an offset to the pointer skb, which now would be outside the kalloked area ?!?!? Maybe I'm missing the obvious, or perhaps I don't quite understand all that is involved here. Correct me if I'm wrong, but if you tried this with plain old malloc you WOULD be in trouble, right?? Can somebody explain this to me, please. Thanks, Radu