On Sat, Jun 8, 2013 at 10:33 AM, Bodo Kaiser <[email protected]> wrote:
> Hello,
>
> can somebody give me an example of how to use the free_callback in
> node::Buffer::New(char * data, size_t length, free_callback callback, void *
> hint)?
>
> https://github.com/joyent/node/blob/master/src/node_buffer.cc#L110
>
> Regards,
> Bodo

// Contrived example that zeroes the memory on release.
struct Allocation {
  size_t size;
  char data[1];
};

void FreeCallback(char* data, void* hint) {
  Allocation* a = static_cast<Allocation*>(hint);
  assert(data == a->data);
  memset(a->data, 0, a->size);
  free(a);
}

void Foo() {
  const size_t size = 1024;
  Allocation* a = malloc(sizeof(*a) + size - 1);
  // NULL check omitted for brevity.
  a->size = size;
  Buffer* buf = Buffer::New(a->data, a->size, FreeCallback, a);
  // do something with the buffer
}

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to