Quake II RTX doxygen  1.0 dev
device_memory_allocator.h File Reference
#include <stdint.h>

Go to the source code of this file.

Classes

struct  DeviceMemory
 

Typedefs

typedef struct DeviceMemory DeviceMemory
 
typedef struct DeviceMemoryAllocator DeviceMemoryAllocator
 

Enumerations

enum  DMAResult { DMA_SUCCESS = 0, DMA_NOT_ENOUGH_MEMORY }
 

Functions

DeviceMemoryAllocatorcreate_device_memory_allocator (VkDevice device)
 
DMAResult allocate_device_memory (DeviceMemoryAllocator *allocator, DeviceMemory *device_memory)
 
void free_device_memory (DeviceMemoryAllocator *allocator, const DeviceMemory *device_memory)
 
void destroy_device_memory_allocator (DeviceMemoryAllocator *allocator)
 

Typedef Documentation

◆ DeviceMemory

typedef struct DeviceMemory DeviceMemory

◆ DeviceMemoryAllocator

Definition at line 37 of file device_memory_allocator.h.

Enumeration Type Documentation

◆ DMAResult

enum DMAResult
Enumerator
DMA_SUCCESS 
DMA_NOT_ENOUGH_MEMORY 

Definition at line 22 of file device_memory_allocator.h.

23 {
24  DMA_SUCCESS = 0,
26 } DMAResult;

Function Documentation

◆ allocate_device_memory()

DMAResult allocate_device_memory ( DeviceMemoryAllocator allocator,
DeviceMemory device_memory 
)

Definition at line 60 of file device_memory_allocator.c.

61 {
62  const uint32_t memory_type = device_memory->memory_type;
63  if (allocator->sub_allocators[memory_type] == NULL)
64  {
65  if (!create_sub_allocator(allocator, memory_type))
66  return DMA_NOT_ENOUGH_MEMORY;
67  }
68 
69  assert(device_memory->size <= ALLOCATOR_CAPACITY);
70 
72  SubAllocator* sub_allocator = allocator->sub_allocators[memory_type];
73 
74  while (result != BA_SUCCESS)
75  {
76  device_memory->memory = sub_allocator->memory;
77 
78  result = buddy_allocator_allocate(sub_allocator->buddy_allocator, device_memory->size,
79  device_memory->alignment, &device_memory->memory_offset);
80 
81  assert(result <= BA_NOT_ENOUGH_MEMORY);
82  if (result == BA_NOT_ENOUGH_MEMORY)
83  {
84  if (sub_allocator->next != NULL)
85  {
86  sub_allocator = sub_allocator->next;
87  }
88  else
89  {
90  if (!create_sub_allocator(allocator, memory_type))
91  return DMA_NOT_ENOUGH_MEMORY;
92  sub_allocator = allocator->sub_allocators[memory_type];
93  }
94  }
95  }
96 
97  return DMA_SUCCESS;
98 }

Referenced by create_invalid_texture(), and vkpt_textures_end_registration().

◆ create_device_memory_allocator()

DeviceMemoryAllocator* create_device_memory_allocator ( VkDevice  device)

Definition at line 48 of file device_memory_allocator.c.

49 {
50  char* memory = malloc(sizeof(DeviceMemoryAllocator));
51 
52  DeviceMemoryAllocator* allocator = (DeviceMemoryAllocator*)memory;
53  memset(allocator, 0, sizeof(DeviceMemoryAllocator));
54  allocator->memory = memory;
55  allocator->device = device;
56 
57  return allocator;
58 }

Referenced by vkpt_textures_initialize().

◆ destroy_device_memory_allocator()

void destroy_device_memory_allocator ( DeviceMemoryAllocator allocator)

Definition at line 110 of file device_memory_allocator.c.

111 {
112  for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++)
113  {
114  SubAllocator* sub_allocator = allocator->sub_allocators[i];
115  while (sub_allocator != NULL)
116  {
117  vkFreeMemory(allocator->device, sub_allocator->memory, NULL);
118  sub_allocator = sub_allocator->next;
119  }
120  }
121 
122  free(allocator->memory);
123 }

Referenced by vkpt_textures_destroy().

◆ free_device_memory()

void free_device_memory ( DeviceMemoryAllocator allocator,
const DeviceMemory device_memory 
)

Definition at line 100 of file device_memory_allocator.c.

101 {
102  SubAllocator* sub_allocator = allocator->sub_allocators[device_memory->memory_type];
103 
104  while (sub_allocator->memory != device_memory->memory)
105  sub_allocator = sub_allocator->next;
106 
107  buddy_allocator_free(sub_allocator->buddy_allocator, device_memory->memory_offset, device_memory->size);
108 }

Referenced by destroy_invalid_texture(), destroy_tex_images(), IMG_ReloadAll(), and textures_destroy_unused_set().

SubAllocator
Definition: device_memory_allocator.c:32
DMAResult
DMAResult
Definition: device_memory_allocator.h:22
buddy_allocator_free
void buddy_allocator_free(BuddyAllocator *allocator, uint64_t offset, uint64_t size)
Definition: buddy_allocator.c:136
DeviceMemoryAllocator::device
VkDevice device
Definition: device_memory_allocator.c:42
SubAllocator::buddy_allocator
BuddyAllocator * buddy_allocator
Definition: device_memory_allocator.c:35
buddy_allocator_allocate
BAResult buddy_allocator_allocate(BuddyAllocator *allocator, uint64_t size, uint64_t alignment, uint64_t *offset)
Definition: buddy_allocator.c:97
DeviceMemoryAllocator
Definition: device_memory_allocator.c:39
DeviceMemoryAllocator::sub_allocators
SubAllocator * sub_allocators[VK_MAX_MEMORY_TYPES]
Definition: device_memory_allocator.c:41
device
static ALCdevice * device
Definition: dynamic.c:53
SubAllocator::next
struct SubAllocator * next
Definition: device_memory_allocator.c:36
BA_SUCCESS
@ BA_SUCCESS
Definition: buddy_allocator.h:24
DeviceMemory::alignment
uint64_t alignment
Definition: device_memory_allocator.h:33
DeviceMemoryAllocator::memory
void * memory
Definition: device_memory_allocator.c:43
create_sub_allocator
int create_sub_allocator(DeviceMemoryAllocator *allocator, uint32_t memory_type)
Definition: device_memory_allocator.c:125
SubAllocator::memory
VkDeviceMemory memory
Definition: device_memory_allocator.c:34
BAResult
BAResult
Definition: buddy_allocator.h:22
DeviceMemory::memory
VkDeviceMemory memory
Definition: device_memory_allocator.h:30
DMA_SUCCESS
@ DMA_SUCCESS
Definition: device_memory_allocator.h:24
DeviceMemory::memory_offset
uint64_t memory_offset
Definition: device_memory_allocator.h:31
DeviceMemory::size
uint64_t size
Definition: device_memory_allocator.h:32
BA_NOT_ENOUGH_MEMORY
@ BA_NOT_ENOUGH_MEMORY
Definition: buddy_allocator.h:25
DeviceMemory::memory_type
uint32_t memory_type
Definition: device_memory_allocator.h:34
ALLOCATOR_CAPACITY
#define ALLOCATOR_CAPACITY
Definition: device_memory_allocator.c:29
DMA_NOT_ENOUGH_MEMORY
@ DMA_NOT_ENOUGH_MEMORY
Definition: device_memory_allocator.h:25