Cynerd commented on code in PR #7198: URL: https://github.com/apache/incubator-nuttx/pull/7198#discussion_r1001556567
########## libs/libc/obstack/lib_obstack_make_room.c: ########## @@ -0,0 +1,79 @@ +/**************************************************************************** + * libs/libc/obstack/lib_obstack_make_room.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <obstack.h> +#include <nuttx/lib/lib.h> +#include <assert.h> + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void obstack_make_room(FAR struct obstack *h, size_t size) +{ + unsigned i; + size_t mask; + + DEBUGASSERT(h); + + if (obstack_room(h) >= size) + { + return; /* No need to allocate anything */ + } + + size_t object_size = obstack_object_size(h); + + size += object_size + sizeof(struct _obstack_chunk); + + /* Note: this is rounding up to the multiple of chunk size that is power of + * two. Thus this creates limitation that chunks can be only power of two. + */ + + mask = h->chunk_size; + for (i = 1; i < sizeof(size_t); i <<= 1) + mask |= mask >> i; + size = (size + mask) & ~mask; + + if (h->chunk == NULL || + h->object_base != (FAR char *)h->chunk + sizeof(struct _obstack_chunk)) + { + /* Allocate new chunk if there is something in the chunk already or if + * there is no chunk yet. + */ + + FAR struct _obstack_chunk *prev = h->chunk; + h->chunk = lib_malloc(size); + h->chunk->prev = prev; Review Comment: Good catch. The growing object would stay here on the old chunk. -- 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