| 2034 | } |
| 2035 | |
| 2036 | static uint32_t malloc_remove(struct malloc_pool_s *pool, uint32_t n) |
| 2037 | { |
| 2038 | uint32_t child, parent, old = n, color; |
| 2039 | if (MA_LEFT(pool, n) == MA_NIL) |
| 2040 | child = MA_RIGHT(pool, n); |
| 2041 | else if (MA_RIGHT(pool, n) == MA_NIL) |
| 2042 | child = MA_LEFT(pool, n); |
| 2043 | else |
| 2044 | { |
| 2045 | uint32_t left; |
| 2046 | n = MA_RIGHT(pool, n); |
| 2047 | while ((left = MA_LEFT(pool, n)) != MA_NIL) |
| 2048 | n = left; |
| 2049 | child = MA_RIGHT(pool, n); |
| 2050 | parent = MA_PARENT(pool, n); |
| 2051 | color = MA_COLOR(pool, n); |
| 2052 | if (child != MA_NIL) |
| 2053 | MA_PARENT(pool, child) = parent; |
| 2054 | if (parent != MA_NIL) |
| 2055 | { |
| 2056 | if (MA_LEFT(pool, parent) == n) |
| 2057 | MA_LEFT(pool, parent) = child; |
| 2058 | else |
| 2059 | MA_RIGHT(pool, parent) = child; |
| 2060 | malloc_fix_invariant(pool, parent); |
| 2061 | } |
| 2062 | else |
| 2063 | pool->root = child; |
| 2064 | if (MA_PARENT(pool, n) == old) |
| 2065 | parent = n; |
| 2066 | MA_PARENT(pool, n) = MA_PARENT(pool, old); |
| 2067 | MA_LEFT(pool, n) = MA_LEFT(pool, old); |
| 2068 | MA_RIGHT(pool, n) = MA_RIGHT(pool, old); |
| 2069 | MA_COLOR(pool, n) = MA_COLOR(pool, old); |
| 2070 | if (MA_PARENT(pool, old) != MA_NIL) |
| 2071 | { |
| 2072 | if (MA_LEFT(pool, MA_PARENT(pool, old)) == old) |
| 2073 | MA_LEFT(pool, MA_PARENT(pool, old)) = n; |
| 2074 | else |
| 2075 | MA_RIGHT(pool, MA_PARENT(pool, old)) = n; |
| 2076 | malloc_fix_invariant(pool, MA_PARENT(pool, old)); |
| 2077 | } |
| 2078 | else |
| 2079 | pool->root = n; |
| 2080 | MA_PARENT(pool, MA_LEFT(pool, old)) = n; |
| 2081 | if (MA_RIGHT(pool, old) != MA_NIL) |
| 2082 | MA_PARENT(pool, MA_RIGHT(pool, old)) = n; |
| 2083 | if (parent) |
| 2084 | { |
| 2085 | left = parent; |
| 2086 | do |
| 2087 | { |
| 2088 | malloc_fix_invariant(pool, left); |
| 2089 | } |
| 2090 | while ((left = MA_PARENT(pool, left)) != MA_NIL); |
| 2091 | } |
| 2092 | goto color; |
| 2093 | } |
no test coverage detected