xiaoxiang781216 commented on code in PR #16664: URL: https://github.com/apache/nuttx/pull/16664#discussion_r2181334514
########## libs/libc/stdlib/lib_atexit.c: ########## @@ -87,15 +87,33 @@ int atexit_register(int type, CODE void (*func)(void), FAR void *arg, { return -ret; } + if (aehead->capacity == 0) { + aehead->capacity = ATEXIT_MAX; + aehead->funcs = (struct atexit_s *)malloc(aehead->capacity * sizeof(struct atexit_s)); Review Comment: don't need handle capactiy equal zero specially, `malloc(size)` equal `realloc(NULL, size)` ########## include/nuttx/atexit.h: ########## @@ -62,7 +62,8 @@ struct atexit_s struct atexit_list_s { int nfuncs; - struct atexit_s funcs[ATEXIT_MAX]; + int capacity; + struct atexit_s funcs[]; Review Comment: you need support the fixed array too, not all projects want to involve the dynamic allocation. ########## libs/libc/misc/lib_cxx_initialize.c: ########## @@ -65,37 +65,30 @@ extern void macho_call_saved_init_funcs(void); void lib_cxx_initialize(void) { #ifdef CONFIG_HAVE_CXXINITIALIZE - static int inited = 0; - - if (inited == 0) Review Comment: lib_cxx_initialize should only run once, otherwise, your global variables will be initialized many times ########## libs/libc/stdlib/lib_atexit.c: ########## @@ -87,15 +87,33 @@ int atexit_register(int type, CODE void (*func)(void), FAR void *arg, { return -ret; } + if (aehead->capacity == 0) { + aehead->capacity = ATEXIT_MAX; + aehead->funcs = (struct atexit_s *)malloc(aehead->capacity * sizeof(struct atexit_s)); + } - if ((idx = aehead->nfuncs) < ATEXIT_MAX) + if ((idx = aehead->nfuncs) < aehead->capacity) { aehead->funcs[idx].type = type; aehead->funcs[idx].func = func; aehead->funcs[idx].arg = arg; aehead->nfuncs++; ret = OK; } + else if ((idx = aehead->nfuncs) >= aehead->capacity) + { + aehead->capacity *= 2; + struct atexit_s * ptr = realloc(aehead->nfuncs, aehead->capacity); Review Comment: Add FAR to ALL pointer. BTW, you must use lib_realloc/lib_free inside libc ########## libs/libc/stdlib/lib_atexit.c: ########## @@ -87,15 +87,33 @@ int atexit_register(int type, CODE void (*func)(void), FAR void *arg, { return -ret; } + if (aehead->capacity == 0) { + aehead->capacity = ATEXIT_MAX; + aehead->funcs = (struct atexit_s *)malloc(aehead->capacity * sizeof(struct atexit_s)); + } - if ((idx = aehead->nfuncs) < ATEXIT_MAX) + if ((idx = aehead->nfuncs) < aehead->capacity) { aehead->funcs[idx].type = type; aehead->funcs[idx].func = func; aehead->funcs[idx].arg = arg; aehead->nfuncs++; ret = OK; } + else if ((idx = aehead->nfuncs) >= aehead->capacity) + { + aehead->capacity *= 2; + struct atexit_s * ptr = realloc(aehead->nfuncs, aehead->capacity); + if (ptr == NULL) { Review Comment: please follow the coding style strictly, run tools/checkpath.sh before submitting the change. ########## libs/libc/stdlib/lib_atexit.c: ########## @@ -87,15 +87,33 @@ int atexit_register(int type, CODE void (*func)(void), FAR void *arg, { return -ret; } + if (aehead->capacity == 0) { + aehead->capacity = ATEXIT_MAX; + aehead->funcs = (struct atexit_s *)malloc(aehead->capacity * sizeof(struct atexit_s)); + } - if ((idx = aehead->nfuncs) < ATEXIT_MAX) + if ((idx = aehead->nfuncs) < aehead->capacity) { aehead->funcs[idx].type = type; aehead->funcs[idx].func = func; aehead->funcs[idx].arg = arg; aehead->nfuncs++; ret = OK; } + else if ((idx = aehead->nfuncs) >= aehead->capacity) + { + aehead->capacity *= 2; + struct atexit_s * ptr = realloc(aehead->nfuncs, aehead->capacity); Review Comment: do you compile the code before? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org