Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Programming Threads > Re: sup****ting...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 19 of 28 Topic 4066 of 4146
Post > Topic >>

Re: sup****ting header-less blocks in memory allocators...

by "Chris M. Thomasson" <no@[EMAIL PROTECTED] > Oct 3, 2008 at 10:41 PM

"Dmitriy V'jukov" <dvyukov@[EMAIL PROTECTED]
> wrote in message 
news:68aa47ab-a67b-46d3-8dac-26fdf4df4f06@[EMAIL PROTECTED]
 Oct 2, 10:55 pm, "Chris M. Thomasson" <n...@[EMAIL PROTECTED]
> wrote:

> > I think C++ is neat, but I have to sup****t C.


> You can mimic this pattern in C:

> [...]

> It's still not global malloc/free, and user still have to supply exact
> size of allocation to free(). But it's C.

Humm... What do you think about the following technique; here is a full 
example program:
_________________________________________________________________________
#include <stdio.h>
#include <stdlib.h>


static void*
thread_alloc_4(size_t size) {
  void* const mem = malloc(size);
  printf("thread_alloc_4(%lu) -> %p\n",
    (unsigned long int)size, mem);
  return mem;
}

static void
thread_free_4(void* mem, size_t size) {
  printf("thread_free_4(%lu) -> %p\n",
    (unsigned long int)size, mem);
  free(mem);
}

static void*
thread_alloc_8(size_t size) {
  void* const mem = malloc(size);
  printf("thread_alloc_8(%lu) -> %p\n",
    (unsigned long int)size, mem);
  return mem;
}

static void
thread_free_8(void* mem, size_t size) {
  printf("thread_free_8(%lu) -> %p\n",
    (unsigned long int)size, mem);
  free(mem);
}


#define MALLOC(mp_ptr) ( \
  (sizeof(*(mp_ptr)) <= 4) \
    ? thread_alloc_4(sizeof(*(mp_ptr))) \
      : (sizeof(*(mp_ptr)) <= 8) \
          ? thread_alloc_8(sizeof(*(mp_ptr))) \
            : malloc(sizeof(*(mp_ptr))) \
)


#define FREE(mp_ptr) ( \
  (sizeof(*(mp_ptr)) <= 4) \
    ? thread_free_4((mp_ptr), sizeof(*(mp_ptr))) \
      : (sizeof(*(mp_ptr)) <= 8) \
          ? thread_free_8((mp_ptr), sizeof(*(mp_ptr))) \
            : free((mp_ptr)) \
)




struct foo1 {
  int a;
};

struct foo2 {
  int a;
  int b;
};

struct foo3 {
  char mem[6];
};

int main() {
  struct foo1* f1 = MALLOC(f1);
  struct foo2* f2 = MALLOC(f2);
  struct foo3* f3 = MALLOC(f3);
  FREE(f3);
  FREE(f2);
  FREE(f1);
  return 0;
}
_________________________________________________________________________



Humm... That should work fine.
 




 28 Posts in Topic:
supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 07:28:39 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 07:30:14 
Re: supporting header-less blocks in memory allocators...
Eric Sosman <Eric.Sosm  2008-10-02 10:55:36 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 10:17:04 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 14:47:48 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 08:41:31 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 09:29:16 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 11:00:02 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 11:50:57 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 11:55:02 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:20:25 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:30:51 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 12:00:46 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:30:09 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:38:26 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:48:45 
Re: supporting header-less blocks in memory allocators...
Michel Decima <michel.  2008-10-03 13:56:34 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 12:07:41 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-03 22:41:41 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 12:24:01 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:32:18 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:35:16 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-03 03:40:04 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-03 03:42:50 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-03 05:52:20 
Re: supporting header-less blocks in memory allocators...
Michel Decima <michel.  2008-10-03 15:28:13 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-03 22:19:59 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-06 00:02:32 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 9:07:03 CST 2008.