Browse Source

Add 'nozero' flag to raw buffer pusher

pull/156/head
Sami Vaarala 10 years ago
parent
commit
5c904dee5a
  1. 5
      src/duk_api_public.h.in
  2. 4
      src/duk_api_stack.c
  3. 2
      src/duk_hbuffer.h
  4. 27
      src/duk_hbuffer_alloc.c
  5. 2
      src/duk_heap_alloc.c

5
src/duk_api_public.h.in

@ -414,7 +414,10 @@ DUK_EXTERNAL_DECL duk_idx_t duk_push_error_object_va_raw(duk_context *ctx, duk_e
#define duk_push_error_object_va(ctx,err_code,fmt,ap) \
duk_push_error_object_va_raw((ctx), (err_code), (const char *) (__FILE__), (duk_int_t) (__LINE__), (fmt), (ap))
DUK_EXTERNAL_DECL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_bool_t dynamic);
#define DUK_BUF_FLAG_DYNAMIC (1 << 0) /* internal flag: dynamic buffer */
#define DUK_BUF_FLAG_NOZERO (1 << 1) /* internal flag: don't zero allocated buffer */
DUK_EXTERNAL_DECL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_small_uint_t flags);
#define duk_push_buffer(ctx,size,dynamic) \
duk_push_buffer_raw((ctx), (size), (dynamic));

4
src/duk_api_stack.c

@ -3410,7 +3410,7 @@ DUK_EXTERNAL duk_idx_t duk_push_error_object_stash(duk_context *ctx, duk_errcode
}
#endif /* DUK_USE_VARIADIC_MACROS */
DUK_EXTERNAL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_bool_t dynamic) {
DUK_EXTERNAL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_small_uint_t flags) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_tval *tv_slot;
duk_hbuffer *h;
@ -3427,7 +3427,7 @@ DUK_EXTERNAL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_bo
DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_BUFFER_TOO_LONG);
}
h = duk_hbuffer_alloc(thr->heap, size, dynamic);
h = duk_hbuffer_alloc(thr->heap, size, flags);
if (!h) {
DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_BUFFER_ALLOC_FAILED);
}

2
src/duk_hbuffer.h

@ -264,7 +264,7 @@ struct duk_hbuffer_dynamic {
* Prototypes
*/
DUK_INTERNAL_DECL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_bool_t dynamic);
DUK_INTERNAL_DECL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags);
DUK_INTERNAL_DECL void *duk_hbuffer_get_dynalloc_ptr(duk_heap *heap, void *ud); /* indirect allocs */
/* dynamic buffer ops */

27
src/duk_hbuffer_alloc.c

@ -4,7 +4,7 @@
#include "duk_internal.h"
DUK_INTERNAL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_bool_t dynamic) {
DUK_INTERNAL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags) {
duk_hbuffer *res = NULL;
duk_size_t alloc_size;
@ -20,29 +20,32 @@ DUK_INTERNAL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk
return NULL;
}
if (dynamic) {
if (flags & DUK_BUF_FLAG_DYNAMIC) {
alloc_size = sizeof(duk_hbuffer_dynamic);
} else {
alloc_size = sizeof(duk_hbuffer_fixed) + size;
DUK_ASSERT(alloc_size >= sizeof(duk_hbuffer_fixed)); /* no wrapping */
}
#ifdef DUK_USE_ZERO_BUFFER_DATA
/* zero everything */
res = (duk_hbuffer *) DUK_ALLOC_ZEROED(heap, alloc_size);
#else
res = (duk_hbuffer *) DUK_ALLOC(heap, alloc_size);
#endif
if (!res) {
goto error;
}
#ifndef DUK_USE_ZERO_BUFFER_DATA
/* if no buffer zeroing, zero the header anyway */
DUK_MEMZERO((void *) res, dynamic ? sizeof(duk_hbuffer_dynamic) : sizeof(duk_hbuffer_fixed));
/* zero everything unless requested not to do so */
#if defined(DUK_USE_ZERO_BUFFER_DATA)
DUK_MEMZERO((void *) res,
(flags & DUK_BUF_FLAG_NOZERO) ?
((flags & DUK_BUF_FLAG_DYNAMIC) ?
sizeof(duk_hbuffer_dynamic) :
sizeof(duk_hbuffer_fixed)) :
alloc_size);
#else
DUK_MEMZERO((void *) res,
(flags & DUK_BUF_FLAG_DYNAMIC) ? sizeof(duk_hbuffer_dynamic) : sizeof(duk_hbuffer_fixed));
#endif
if (dynamic) {
if (flags & DUK_BUF_FLAG_DYNAMIC) {
duk_hbuffer_dynamic *h = (duk_hbuffer_dynamic *) res;
void *ptr;
if (size > 0) {
@ -70,7 +73,7 @@ DUK_INTERNAL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk
DUK_HBUFFER_SET_SIZE(res, size);
DUK_HEAPHDR_SET_TYPE(&res->hdr, DUK_HTYPE_BUFFER);
if (dynamic) {
if (flags & DUK_BUF_FLAG_DYNAMIC) {
DUK_HBUFFER_SET_DYNAMIC(res);
}
DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, &res->hdr);

2
src/duk_heap_alloc.c

@ -898,7 +898,7 @@ duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
DUK_DD(DUK_DDPRINT("HEAP: INIT LOG BUFFER"));
res->log_buffer = (duk_hbuffer_dynamic *) duk_hbuffer_alloc(res,
DUK_BI_LOGGER_SHORT_MSG_LIMIT,
1 /*dynamic*/);
DUK_BUF_FLAG_DYNAMIC /*flags*/);
if (!res->log_buffer) {
goto error;
}

Loading…
Cancel
Save