node, buffers and realloc

216 views
Skip to first unread message

Seiji Sam Lee

unread,
Aug 30, 2012, 5:12:27 PM8/30/12
to nod...@googlegroups.com
Apologize if this is a old-issue but, why Buffer has its size static?

Why don't use realloc to resize its capacity?

I have done a addon of resized buffers and it works perfectly 8-|

grettings.


Ben Noordhuis

unread,
Aug 30, 2012, 5:48:36 PM8/30/12
to nod...@googlegroups.com
On Thu, Aug 30, 2012 at 11:12 PM, Seiji Sam Lee <seiji...@gmail.com> wrote:
> Apologize if this is a old-issue but, why Buffer has its size static?
>
> Why don't use realloc to resize its capacity?

Because bad things would happen if the code below was legal:

var buf = new Buffer(1024);
fs.read(fd, buf, buf.length, cb);
buf.resize(32);

codepilot Account

unread,
Aug 31, 2012, 1:23:02 PM8/31/12
to nod...@googlegroups.com
Buffers can in a way be resized, use slice to make smaller, and concat to make bigger. I doubt slice frees anything, but concat does allocate if it needs to.


--
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 nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Seiji Sam Lee

unread,
Sep 1, 2012, 1:09:17 PM9/1/12
to nod...@googlegroups.com
Right! great argument, but ....

I have done a buffer with a hidden realloc, it born with size zero and realloc when it is needed. There aren't a exlicit realloc method. Would be util for the community?

Seiji Sam Lee

unread,
Sep 1, 2012, 1:11:07 PM9/1/12
to nod...@googlegroups.com
too much slow!!

Nathan Rajlich

unread,
Sep 1, 2012, 1:49:23 PM9/1/12
to nod...@googlegroups.com
My "ref" module has a "reinterpret()"[1] function which takes a Buffer
instance and returns a new Buffer instance that points to the same
memory address, but has an arbitrary "length" that you specify. This
is both dangerous and necessary sometimes when dealing with ffi stuff.

[1]: http://tootallnate.github.com/ref/#exports-reinterpret
Reply all
Reply to author
Forward
0 new messages