diff options
author | Dave Barach <dave@barachs.net> | 2016-10-13 10:53:26 -0400 |
---|---|---|
committer | Dave Barach <dave@barachs.net> | 2016-10-13 10:56:20 -0400 |
commit | 241e52222e5e5579516494a8d979b6cc282f4799 (patch) | |
tree | 9e188c588358eb559529d51bf97bd06f46f0c843 | |
parent | 0683c9cc130d45f1246be78fa4ebf3f8d7f322bb (diff) |
Add clib_mem_alloc_or_null(...)
Change-Id: I5177d6d3349384beb551b4f2f52b30b044ce335b
Signed-off-by: Dave Barach <dave@barachs.net>
-rw-r--r-- | svm/svm.h | 3 | ||||
-rw-r--r-- | vlib/vlib/node.c | 3 | ||||
-rw-r--r-- | vppinfra/vppinfra/mem.h | 34 | ||||
-rw-r--r-- | vppinfra/vppinfra/vec.c | 8 |
4 files changed, 36 insertions, 12 deletions
diff --git a/svm/svm.h b/svm/svm.h index 5b95abb66ba..0b87dbcbc64 100644 --- a/svm/svm.h +++ b/svm/svm.h @@ -149,7 +149,8 @@ svm_mem_alloc_aligned_at_offset (svm_region_t * rp, pthread_mutex_lock (&rp->mutex); oldheap = clib_mem_set_heap (rp->data_heap); - rv = clib_mem_alloc_aligned_at_offset (size, align, offset); + rv = clib_mem_alloc_aligned_at_offset (size, align, offset, + 1 /* yes, call os_out_of_memory */ ); clib_mem_set_heap (oldheap); pthread_mutex_unlock (&rp->mutex); return (rv); diff --git a/vlib/vlib/node.c b/vlib/vlib/node.c index e18567b4184..69bb07f6e41 100644 --- a/vlib/vlib/node.c +++ b/vlib/vlib/node.c @@ -375,7 +375,8 @@ register_node (vlib_main_t * vm, vlib_node_registration_t * r) p = clib_mem_alloc_aligned_at_offset (sizeof (p[0]) + (1 << log2_n_stack_bytes), - STACK_ALIGN, STRUCT_OFFSET_OF (vlib_process_t, stack)); + STACK_ALIGN, STRUCT_OFFSET_OF (vlib_process_t, stack), + 0 /* no, don't call os_out_of_memory */ ); if (p == 0) clib_panic ("failed to allocate process stack (%d bytes)", 1 << log2_n_stack_bytes); diff --git a/vppinfra/vppinfra/mem.h b/vppinfra/vppinfra/mem.h index d88254562c0..1260eab28c0 100644 --- a/vppinfra/vppinfra/mem.h +++ b/vppinfra/vppinfra/mem.h @@ -67,9 +67,10 @@ clib_mem_set_per_cpu_heap (u8 * new_heap) return old; } -/* Memory allocator which returns null when it fails. */ +/* Memory allocator which may call os_out_of_memory() if it fails */ always_inline void * -clib_mem_alloc_aligned_at_offset (uword size, uword align, uword align_offset) +clib_mem_alloc_aligned_at_offset (uword size, uword align, uword align_offset, + int os_out_of_memory_on_failure) { void *heap, *p; uword offset, cpu; @@ -97,25 +98,46 @@ clib_mem_alloc_aligned_at_offset (uword size, uword align, uword align_offset) } else { - os_out_of_memory (); + if (os_out_of_memory_on_failure) + os_out_of_memory (); return 0; } } -/* Memory allocator which returns null when it fails. */ +/* Memory allocator which calls os_out_of_memory() when it fails */ always_inline void * clib_mem_alloc (uword size) { return clib_mem_alloc_aligned_at_offset (size, /* align */ 1, - /* align_offset */ 0); + /* align_offset */ 0, + /* os_out_of_memory */ 1); } always_inline void * clib_mem_alloc_aligned (uword size, uword align) { - return clib_mem_alloc_aligned_at_offset (size, align, /* align_offset */ 0); + return clib_mem_alloc_aligned_at_offset (size, align, /* align_offset */ 0, + /* os_out_of_memory */ 1); } +/* Memory allocator which calls os_out_of_memory() when it fails */ +always_inline void * +clib_mem_alloc_or_null (uword size) +{ + return clib_mem_alloc_aligned_at_offset (size, /* align */ 1, + /* align_offset */ 0, + /* os_out_of_memory */ 0); +} + +always_inline void * +clib_mem_alloc_aligned_or_null (uword size, uword align) +{ + return clib_mem_alloc_aligned_at_offset (size, align, /* align_offset */ 0, + /* os_out_of_memory */ 0); +} + + + /* Memory allocator which panics when it fails. Use macro so that clib_panic macro can expand __FUNCTION__ and __LINE__. */ #define clib_mem_alloc_aligned_no_fail(size,align) \ diff --git a/vppinfra/vppinfra/vec.c b/vppinfra/vppinfra/vec.c index f711679b0a4..2d7ae1d4dc6 100644 --- a/vppinfra/vppinfra/vec.c +++ b/vppinfra/vppinfra/vec.c @@ -56,9 +56,8 @@ vec_resize_allocate_memory (void *v, if (!v) { - new = - clib_mem_alloc_aligned_at_offset (data_bytes, data_align, - header_bytes); + new = clib_mem_alloc_aligned_at_offset (data_bytes, data_align, header_bytes, 1 /* yes, call os_out_of_memory */ + ); data_bytes = clib_mem_size (new); memset (new, 0, data_bytes); v = new + header_bytes; @@ -84,7 +83,8 @@ vec_resize_allocate_memory (void *v, new = clib_mem_alloc_aligned_at_offset (new_alloc_bytes, data_align, - header_bytes); + header_bytes, + 1 /* yes, call os_out_of_memory */ ); /* FIXME fail gracefully. */ if (!new) |