source: golgotha/src/render/alloc.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 2.7 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "tmanage.hh"
10#include "threads/threads.hh"
11
12/*
13static i4_critical_section_class r1_texture_alloc_lock;
14
15// Mmeory allocation might be called from a seperate thread so you should be aware
16// of what the main program sees in memory at each stage of allocation.
17// Since Garbage collection & free's will not occur while the texture loading thread
18// (async_read) is active, this should not present any problems.
19
20r1_texture_node_struct *r1_texture_manager_class::free_node::alloc_some(sw32 alloc_size,
21                                                                        free_node *&last_ptr)
22{
23  alloc_size=(alloc_size+3)&(~3);
24  r1_texture_node_struct *a=(r1_texture_node_struct *)this;
25 
26  // is there enough space to split the free node?
27  if (alloc_size+sizeof(r1_texture_node_struct)+4>size)
28  {
29    a->set_size((sw32)size+(sw32)sizeof(free_node)-(sw32)sizeof(r1_texture_node_struct));
30    last_ptr=next;
31
32  }
33  else
34  {
35    // first setup the new free node
36    free_node *n=(free_node *)((w8 *)this+sizeof(r1_texture_node_struct)+alloc_size);
37    n->next=next;
38    n->size=(sw32)size-(sw32)sizeof(r1_texture_node_struct)-(sw32)alloc_size;
39
40    a->set_size(alloc_size);
41
42    // set the last pointer to the new me
43    last_ptr=n;
44  }
45
46  return a;
47}
48
49
50r1_texture_node_struct *r1_texture_manager_class::alloc(sw32 size)
51
52  free_node *last=0, *next;
53  r1_texture_node_struct *ret;
54 
55  r1_texture_alloc_lock.lock();
56 
57  if (!first_free)
58  {
59    r1_texture_alloc_lock.unlock();
60    return 0;
61  }
62 
63  for (free_node *f=first_free; f!=end; f=f->next)
64  {
65    if (f->size>=size+(sw32)sizeof(r1_texture_node_struct)-(sw32)sizeof(free_node))
66    {
67      if (last)
68        ret=f->alloc_some(size, last->next);
69      else 
70        ret=f->alloc_some(size, first_free);
71
72      ret->vram_handle=0;
73
74      r1_texture_alloc_lock.unlock();
75      return ret;
76    }
77    last=f;
78  }
79 
80  r1_texture_alloc_lock.unlock();
81  return 0;     
82}
83
84i4_bool r1_texture_manager_class::can_alloc(sw32 size)
85{
86  if (!first_free)
87    return i4_F;
88
89  for (free_node *f=first_free; f!=end; f=f->next)
90        if (f->size>=size+(sw32)sizeof(r1_texture_node_struct)-(sw32)sizeof(free_node))
91      return i4_T;
92  return i4_F;
93}
94
95*/
Note: See TracBrowser for help on using the repository browser.