summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorIvan Maidanski <ivmai@mail.ru>2023-04-26 09:27:50 +0300
committerIvan Maidanski <ivmai@mail.ru>2023-04-26 17:31:27 +0300
commitfef1ce084af6dc3a0051025c12e04a1eaa5eb1b0 (patch)
treec69e8c606525325489f605aa6e7a488d1bb63d27 /include
parent3e93d05a20d0da777765078836c69cc60472187d (diff)
downloadbdwgc-fef1ce084af6dc3a0051025c12e04a1eaa5eb1b0.tar.gz
New API for more optimal usage of GC_calloc_explicitly_typed
If the client needs to allocate many typed object arrays of same layout and amount of elements, then "calloc" descriptor could be created once (by GC_calloc_prepare_explicitly_typed) followed by multiple allocations (by GC_calloc_do_explicitly_typed) referring to the same descriptor. * include/gc/gc_typed.h (GC_CALLOC_TYPED_DESCR_WORDS): New macro. * include/gc/gc_typed.h (GC_calloc_typed_descr_s): New struct type. * include/gc/gc_typed.h (GC_calloc_prepare_explicitly_typed, GC_calloc_do_explicitly_typed): New function declaration. * tests/gctest.c [!NO_TYPED_TEST && !GC_DEBUG] (typed_test): Define ctd_l local variable; call GC_calloc_prepare_explicitly_typed() before loop; call GC_calloc_do_explicitly_typed() instead of GC_CALLOC_EXPLICITLY_TYPED(1001). * typd_mlc.c (GC_calloc_typed_descr_s.alloc_lb): Change type from size_t to word. * typd_mlc.c (GC_calloc_typed_descr_s.descr_type): Change type from int to signed_word. * typd_mlc.c (GC_calloc_prepare_explicitly_typed, GC_calloc_do_explicitly_typed): Change from STATIC to GC_API; add ctd_sz argument; check ctd_sz in assertion; add casts for alloc_lb field. * typd_mlc.c (GC_calloc_prepare_explicitly_typed): Add static assertion about size of GC_calloc_typed_descr_s and GC_CALLOC_TYPED_DESCR_WORDS; change return type from void to it. * typd_mlc.c (GC_calloc_explicitly_typed): Pass sizeof(ctd) to GC_calloc_prepare_explicitly_typed(), GC_calloc_do_explicitly_typed().
Diffstat (limited to 'include')
-rw-r--r--include/gc/gc_typed.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/include/gc/gc_typed.h b/include/gc/gc_typed.h
index 1de355fe..a9a82931 100644
--- a/include/gc/gc_typed.h
+++ b/include/gc/gc_typed.h
@@ -104,6 +104,37 @@ GC_API GC_ATTR_MALLOC GC_ATTR_CALLOC_SIZE(1, 2) void * GC_CALL
/* bitmap of d multiplied by sizeof GC_word. */
/* Returned object is cleared. */
+#define GC_CALLOC_TYPED_DESCR_WORDS 8
+
+#ifdef GC_BUILD
+ struct GC_calloc_typed_descr_s;
+#else
+ struct GC_calloc_typed_descr_s {
+ GC_word opaque[GC_CALLOC_TYPED_DESCR_WORDS];
+ };
+#endif
+
+GC_API int GC_CALL GC_calloc_prepare_explicitly_typed(
+ struct GC_calloc_typed_descr_s * /* pctd */,
+ size_t /* sizeof_ctd */, size_t /* nelements */,
+ size_t /* element_size_in_bytes */, GC_descr);
+ /* This is same as GC_calloc_explicitly_typed but more optimal */
+ /* in terms of the performance and memory usage if the client */
+ /* needs to allocate multiple typed object arrays with the */
+ /* same layout and number of elements. The client should call */
+ /* it to be prepared for the subsequent allocations by */
+ /* GC_calloc_do_explicitly_typed, one or many. The result of */
+ /* the preparation is stored to *pctd, even in case of */
+ /* a failure. The prepared structure could be just dropped */
+ /* when no longer needed. Returns 0 on failure, 1 on success; */
+ /* the result could be ignored (as it is also stored to *pctd */
+ /* and checked later by GC_calloc_do_explicitly_typed). */
+
+GC_API GC_ATTR_MALLOC void * GC_CALL GC_calloc_do_explicitly_typed(
+ const struct GC_calloc_typed_descr_s * /* pctd */,
+ size_t /* sizeof_ctd */);
+ /* The actual object allocation for the prepared description. */
+
#ifdef GC_DEBUG
# define GC_MALLOC_EXPLICITLY_TYPED(bytes, d) ((void)(d), GC_MALLOC(bytes))
# define GC_MALLOC_EXPLICITLY_TYPED_IGNORE_OFF_PAGE(bytes, d) \