1 /*
2  * Copyright (c) 2012, 2013 ARM Ltd
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the company may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "nano-malloc.h"
30 
31 /*
32  * Algorithm:
33  *  Maintain a global free chunk_t single link list, headed by global
34  *  variable __malloc_free_list.
35  *  When free, insert the to-be-freed chunk_t into free list. The place to
36  *  insert should make sure all chunks are sorted by address from low to
37  *  high.  Then merge with neighbor chunks if adjacent.
38  */
39 
40 void
free(void * free_p)41 free (void * free_p)
42 {
43     chunk_t     *p_to_free;
44     chunk_t     **p, *r;
45 
46     if (free_p == NULL) return;
47 
48     p_to_free = ptr_to_chunk(free_p);
49     p_to_free->next = NULL;
50 
51 #if MALLOC_DEBUG
52     __malloc_validate_block(p_to_free);
53 #endif
54 
55     MALLOC_LOCK;
56 
57     for (p = &__malloc_free_list; (r = *p) != NULL; p = &r->next)
58     {
59 	/* Insert in address order */
60 	if (p_to_free <= r) {
61 
62             /* Check for double free */
63             if (p_to_free == r)
64             {
65                 errno = ENOMEM;
66                 goto unlock;
67             }
68 
69 	    break;
70         }
71 
72 	/* Merge blocks together */
73 	if (chunk_after(r) == p_to_free)
74 	{
75 	    *_size_ref(r) += _size(p_to_free);
76 	    p_to_free = r;
77 	    r = r->next;
78 	    goto no_insert;
79 	}
80 
81     }
82 
83     p_to_free->next = r;
84     *p = p_to_free;
85 
86 no_insert:
87 
88     /* Merge blocks together */
89     if (chunk_after(p_to_free) == r)
90     {
91 #ifdef __GNUC__
92 #pragma GCC diagnostic push
93 #pragma GCC diagnostic ignored "-Wpragmas"
94 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
95 #pragma GCC diagnostic ignored "-Wanalyzer-null-dereference"
96 #endif
97 	*_size_ref(p_to_free) += _size(r);
98 #ifdef __GNUC__
99 #pragma GCC diagnostic pop
100 #endif
101 	p_to_free->next = r->next;
102     }
103 
104 unlock:
105     MALLOC_UNLOCK;
106 }
107 
108 #ifdef _HAVE_ALIAS_ATTRIBUTE
109 #ifndef __clang__
110 #pragma GCC diagnostic ignored "-Wmissing-attributes"
111 #endif
112 __strong_reference(free, __malloc_free);
113 __strong_reference(free, cfree);
114 #else
cfree(void * ptr)115 void cfree(void * ptr)
116 {
117     free(ptr);
118 }
119 #endif
120