Skip to content
Snippets Groups Projects
Commit c01e9805 authored by Jammer, Tim's avatar Jammer, Tim
Browse files

Added Alloc Call Class for easy manipulation of Allocations

parent e77b3d66
No related branches found
No related tags found
No related merge requests found
#! /usr/bin/python3
alloc_template = """
@{NAME}@ = (@{TYPE}@*) @{FUNCTION}@(@{NUM}@ @{SEP}@ sizeof(@{TYPE}@));
"""
"""
Class Overview:
The `AllocCall` class is a helper for creationg allocation calls (with malloc or calloc)
Methods:
- `__init__(self)`: Initializes a new Alloc Call
- `__str__(self)`: Converts the AllocCall instance to a string, replacing placeholders.
- `set_num_elements(self, num_elements)`: Sets number of elements to allocate
- `set_type(self, type)`: Sets the type of allocated elements
- `set_name(self, name)`: Sets the name of allocated variable
- `set_use_calloc(self)`: Use calloc
- `set_use_malloc(self)`: Use malloc
"""
class AllocCall:
def __init__(self, type, num_elements, name="buf", use_malloc=False):
"""
Creates a New allocation Call
Args:
type: Type to allocate (int not MPI_INT)
num_elements: number of buffer elements
name: name of buffer variable
use_malloc: True: use Malloc, False: use calloc for allocation
"""
self._use_malloc = use_malloc
self._type = type
self._num_elements = num_elements
self._name = name
def __str__(self):
if self._use_malloc:
delim = '*'
func = "malloc"
else:
delim = ','
func = "calloc"
return (alloc_template
.replace("@{NAME}@", self._name)
.replace("@{TYPE}@", self._type)
.replace("@{FUNCTION}@", func)
.replace("@{NUM}@", self._num_elements)
.replace("@{SEP}", delim))
def set_num_elements(self, num_elements):
self._num_elements = num_elements
def set_name(self, name):
self._name = name
def set_type(self, type):
self._type = type
def set_use_malloc(self):
self._use_malloc = True
def set_use_calloc(self):
self._use_malloc = False
def get_num_elements(self):
return self._num_elements
def get_name(self):
return self._name
def get_type(self):
return self._type
def get_use_malloc(self):
return self._use_malloc
def get_free(alloc_call):
assert isinstance(alloc_call, AllocCall)
return "free(" + alloc_call.get_name() + ");\n"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment