mirror of
https://github.com/apache/nuttx.git
synced 2026-05-10 15:30:25 +08:00
977f9ed4b8
The original condition incorrectly used &h->chunk instead of h->chunk in the calculation whether the object is in the chunk. This could lead to the wrong behavior as the first branch gave incorrect result and thus sometimes the entire obstack was freed even though object was not NULL. The commit also simplifies the logic, we can use pointer arithmetic here and just do h->chunk + 1 as it gives the same result as (FAR char *)h->chunk + sizeof(struct _obstack_chunk). This saves unnecessary cast and sizeof. The second branch should be less than or equal, not just less than. This ensures the object is correctly located in the chunk even after previous obstack_finish was called. Signed-off-by: Michal Lenc <michallenc@seznam.cz> Co-authored-by: Karel Kočí <kkoci@elektroline.cz>
84 lines
3.1 KiB
C
84 lines
3.1 KiB
C
/****************************************************************************
|
|
* libs/libc/obstack/lib_obstack_free.c
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* 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>
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: obstack_free
|
|
*
|
|
* Description:
|
|
* Free objects (and everything allocated in the specified obstack more
|
|
* recently than object). You can pass NULL to free everything.
|
|
* The buffer the allocated object was preset is kept and thus can be
|
|
* immediately reused for growing. The only exception for this is when NULL
|
|
* is passed as in such case even the last buffer is freed.
|
|
*
|
|
* Input Parameters:
|
|
* h: pointer to the handle object belongs to
|
|
* object: the pointer to the object or NULL
|
|
*
|
|
****************************************************************************/
|
|
|
|
void obstack_free(FAR struct obstack *h, FAR void *object)
|
|
{
|
|
FAR struct _obstack_chunk *prev;
|
|
|
|
while (h->chunk)
|
|
{
|
|
/* Object has to be after chunk + chunk' header. We can use pointer
|
|
* arithmetic here as h->chunk + 1 is the same as
|
|
* (FAR char *)h->chunk + sizeof(struct _obstack_chunk)
|
|
*/
|
|
|
|
if (object >= (FAR void *)(h->chunk + 1)
|
|
&& object <= (FAR void *)h->chunk->limit)
|
|
{
|
|
/* The object is in this chunk so just move object base.
|
|
* Note: this keeps the last chunk allocated. This is desirable
|
|
* behavior as we can decide if we want to reuse it by either
|
|
* passing NULL to free everything or the first returned object to
|
|
* keep the chunk allocated.
|
|
*/
|
|
|
|
h->object_base = object;
|
|
h->next_free = object;
|
|
return;
|
|
}
|
|
|
|
prev = h->chunk->prev;
|
|
lib_free(h->chunk);
|
|
h->chunk = prev;
|
|
}
|
|
|
|
h->object_base = NULL;
|
|
h->next_free = NULL;
|
|
}
|