+
+/* Release the memory for object P. */
+
+void
+ggc_free (void *p)
+{
+ page_entry *pe = lookup_page_table_entry (p);
+ size_t order = pe->order;
+ size_t size = OBJECT_SIZE (order);
+
+#ifdef GATHER_STATISTICS
+ ggc_free_overhead (p);
+#endif
+
+ if (GGC_DEBUG_LEVEL >= 3)
+ fprintf (G.debug_file,
+ "Freeing object, actual size=%lu, at %p on %p\n",
+ (unsigned long) size, p, (void *) pe);
+
+#ifdef ENABLE_GC_CHECKING
+ /* Poison the data, to indicate the data is garbage. */
+ VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (p, size));
+ memset (p, 0xa5, size);
+#endif
+ /* Let valgrind know the object is free. */
+ VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (p, size));
+
+#ifdef ENABLE_GC_ALWAYS_COLLECT
+ /* In the completely-anal-checking mode, we do *not* immediately free
+ the data, but instead verify that the data is *actually* not
+ reachable the next time we collect. */
+ {
+ struct free_object *fo = xmalloc (sizeof (struct free_object));
+ fo->object = p;
+ fo->next = G.free_object_list;
+ G.free_object_list = fo;
+ }
+#else
+ {
+ unsigned int bit_offset, word, bit;
+
+ G.allocated -= size;
+
+ /* Mark the object not-in-use. */
+ bit_offset = OFFSET_TO_BIT (((const char *) p) - pe->page, order);
+ word = bit_offset / HOST_BITS_PER_LONG;
+ bit = bit_offset % HOST_BITS_PER_LONG;
+ pe->in_use_p[word] &= ~(1UL << bit);
+
+ if (pe->num_free_objects++ == 0)
+ {
+ page_entry *p, *q;
+
+ /* If the page is completely full, then it's supposed to
+ be after all pages that aren't. Since we've freed one
+ object from a page that was full, we need to move the
+ page to the head of the list.
+
+ PE is the node we want to move. Q is the previous node
+ and P is the next node in the list. */
+ q = pe->prev;
+ if (q && q->num_free_objects == 0)
+ {
+ p = pe->next;
+
+ q->next = p;
+
+ /* If PE was at the end of the list, then Q becomes the
+ new end of the list. If PE was not the end of the
+ list, then we need to update the PREV field for P. */
+ if (!p)
+ G.page_tails[order] = q;
+ else
+ p->prev = q;
+
+ /* Move PE to the head of the list. */
+ pe->next = G.pages[order];
+ pe->prev = NULL;
+ G.pages[order]->prev = pe;
+ G.pages[order] = pe;
+ }
+
+ /* Reset the hint bit to point to the only free object. */
+ pe->next_bit_hint = bit_offset;
+ }
+ }
+#endif
+}