mesa: add AllowGLSLCrossStageInterpolationMismatch workaround
[mesa.git] / src / mesa / main / mm.c
index e5f445889f9a680ad36471a5996e25c18a1ec997..473e90fc2d5be879f13eb2ad7346ccb5494b60d2 100644 (file)
  *
  */
 
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "compiler.h"
 #include "mm.h"
 
 
@@ -43,7 +48,7 @@ mmDumpMemInfo(const struct mem_block *heap)
       fprintf(stderr, "\nFree list:\n");
 
       for(p = heap->next_free; p != heap; p = p->next_free) {
-        fprintf(stderr, "  Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size,
+        fprintf(stderr, " FREE Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size,
                 p->free ? 'F':'.',
                 p->reserved ? 'R':'.');
       }
@@ -53,20 +58,20 @@ mmDumpMemInfo(const struct mem_block *heap)
 }
 
 struct mem_block *
-mmInit(int ofs, int size)
+mmInit(unsigned ofs, unsigned size)
 {
    struct mem_block *heap, *block;
   
-   if (size <= 0
+   if (!size
       return NULL;
 
-   heap = (struct mem_block *) _mesa_calloc(sizeof(struct mem_block));
+   heap = calloc(1, sizeof(struct mem_block));
    if (!heap) 
       return NULL;
    
-   block = (struct mem_block *) _mesa_calloc(sizeof(struct mem_block));
+   block = calloc(1, sizeof(struct mem_block));
    if (!block) {
-      _mesa_free(heap);
+      free(heap);
       return NULL;
    }
 
@@ -85,20 +90,20 @@ mmInit(int ofs, int size)
    block->size = size;
    block->free = 1;
 
-   return (struct mem_block *)heap;
+   return heap;
 }
 
 
 static struct mem_block *
 SliceBlock(struct mem_block *p, 
-           int startofs, int size, 
-           int reserved, int alignment)
+           unsigned startofs, unsigned size, 
+           unsigned reserved, unsigned alignment)
 {
    struct mem_block *newblock;
 
    /* break left  [p, newblock, p->next], then p = newblock */
    if (startofs > p->ofs) {
-      newblock = (struct mem_block*) _mesa_calloc(sizeof(struct mem_block));
+      newblock = calloc(1, sizeof(struct mem_block));
       if (!newblock)
         return NULL;
       newblock->ofs = startofs;
@@ -120,9 +125,9 @@ SliceBlock(struct mem_block *p,
       p = newblock;
    }
 
-   /* break right, also [p, newblock, p->next], then p = newblock*/
+   /* break right, also [p, newblock, p->next] */
    if (size < p->size) {
-      newblock = (struct mem_block*) _mesa_calloc(sizeof(struct mem_block));
+      newblock = calloc(1, sizeof(struct mem_block));
       if (!newblock)
         return NULL;
       newblock->ofs = startofs + size;
@@ -151,20 +156,23 @@ SliceBlock(struct mem_block *p,
    p->next_free->prev_free = p->prev_free;
    p->prev_free->next_free = p->next_free;
 
+   p->next_free = 0;
+   p->prev_free = 0;
+
    p->reserved = reserved;
    return p;
 }
 
 
 struct mem_block *
-mmAllocMem(struct mem_block *heap, int size, int align2, int startSearch)
+mmAllocMem(struct mem_block *heap, unsigned size, unsigned align2, unsigned startSearch)
 {
    struct mem_block *p;
-   const int mask = (1 << align2)-1;
-   int startofs = 0;
-   int endofs;
+   const unsigned mask = (1 << align2)-1;
+   unsigned startofs = 0;
+   unsigned endofs;
 
-   if (!heap || align2 < 0 || size <= 0)
+   if (!heap || !size)
       return NULL;
 
    for (p = heap->next_free; p != heap; p = p->next_free) {
@@ -182,6 +190,7 @@ mmAllocMem(struct mem_block *heap, int size, int align2, int startSearch)
    if (p == heap) 
       return NULL;
 
+   assert(p->free);
    p = SliceBlock(p,startofs,size,0,mask+1);
 
    return p;
@@ -189,11 +198,11 @@ mmAllocMem(struct mem_block *heap, int size, int align2, int startSearch)
 
 
 struct mem_block *
-mmFindBlock(struct mem_block *heap, int start)
+mmFindBlock(struct mem_block *heap, unsigned start)
 {
    struct mem_block *p;
 
-   for (p = heap->next_free; p != heap; p = p->next_free) {
+   for (p = heap->next; p != heap; p = p->next) {
       if (p->ofs == start) 
         return p;
    }
@@ -202,7 +211,7 @@ mmFindBlock(struct mem_block *heap, int start)
 }
 
 
-static INLINE int
+static inline int
 Join2Blocks(struct mem_block *p)
 {
    /* XXX there should be some assertions here */
@@ -211,6 +220,8 @@ Join2Blocks(struct mem_block *p)
 
    if (p->free && p->next->free) {
       struct mem_block *q = p->next;
+
+      assert(p->ofs + p->size == q->ofs);
       p->size += q->size;
 
       p->next = q->next;
@@ -219,7 +230,7 @@ Join2Blocks(struct mem_block *p)
       q->next_free->prev_free = q->prev_free; 
       q->prev_free->next_free = q->next_free;
      
-      _mesa_free(q);
+      free(q);
       return 1;
    }
    return 0;
@@ -247,7 +258,7 @@ mmFreeMem(struct mem_block *b)
    b->prev_free->next_free = b;
 
    Join2Blocks(b);
-   if (b->prev)
+   if (b->prev != b->heap)
       Join2Blocks(b->prev);
 
    return 0;
@@ -264,9 +275,9 @@ mmDestroy(struct mem_block *heap)
 
    for (p = heap->next; p != heap; ) {
       struct mem_block *next = p->next;
-      _mesa_free(p);
+      free(p);
       p = next;
    }
 
-   _mesa_free(heap);
+   free(heap);
 }