FreeRTOS Tetris
Data Structures | Macros | Functions | Variables
TUM_Utils.c File Reference

Utilities required by other TUM_XXX files. More...

#include <sys/syscall.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <regex.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <assert.h>
#include <dirent.h>
#include "TUM_Utils.h"
#include "EmulatorConfig.h"
#include <stdatomic.h>
Include dependency graph for TUM_Utils.c:

Data Structures

struct  ring_buf
 

Macros

#define _GNU_SOURCE
 
#define CAST_RBUF(rbuf)   ((struct ring_buf *)rbuf)
 
#define INCLUDE_DIR_NAMES   0b1
 

Functions

int tumUtilIsCurGLThread (void)
 Checks if the calling thread is the thread that currently holds the GL context. More...
 
void tumUtilSetGLThread (void)
 The calling thread is registered as holding the current GL context.
 
char * tumUtilPrependPath (char *path, char *file)
 Prepends a path string to a filename. More...
 
char * tumUtilGetBinFolderPath (char *bin_path)
 Gets the execution folder of the current program, assumes that program is executing from a folder "bin". More...
 
static char * recurseDirName (char *dir_name, char *filename, char flags)
 Finds and returns the full path of a file searched for in a target directory and its sub-directories. More...
 
static FILE * recurseDirFile (char *dir_name, char *filename, const char *mode)
 
static char * tumUtilFindResourceDirectory (void)
 
FILE * tumUtilFindResource (char *resource_name, const char *mode)
 Searches for a file in the RESOURCES_DIRECTORY and returns a FILE * if found. More...
 
char * tumUtilFindResourcePath (char *resource_name)
 Similar to tumUtilFindResource() only returning the file's path instead of the opened FILE's reference. More...
 
static void inc_buf (rbuf_handle_t rbuf)
 
static void dec_buf (rbuf_handle_t rbuf)
 
rbuf_handle_t rbuf_init (size_t item_size, size_t item_count)
 Initialized a ring buffer object with a certain number of objects of a given size. More...
 
rbuf_handle_t rbuf_init_static (size_t item_size, size_t item_count, void *buffer)
 Initialized a ring buffer object with a certain number of objects of a given size into a statically allocated buffer. More...
 
void rbuf_free (rbuf_handle_t rbuf)
 Frees a ring buffer. More...
 
void rbuf_reset (rbuf_handle_t rbuf)
 Resets the ring buffer to it's initial state. More...
 
int rbuf_put_buffer (rbuf_handle_t rbuf)
 Used when a reference to the next buffer item is already filled, incrementing the next buffer item should an item need to be retrieved. More...
 
int rbuf_put (rbuf_handle_t rbuf, void *data)
 Fills the next available buffer slot, if a slot is free. More...
 
int rbuf_fput (rbuf_handle_t rbuf, void *data)
 Fills the next available buffer, overwriting data if the ring buffer is full. More...
 
void * rbuf_get_buffer (rbuf_handle_t rbuf)
 Returns a reference to the data of the next ring buffer entry. More...
 
int rbuf_get (rbuf_handle_t rbuf, void *data)
 Returns a copy of the next buffer item's data. More...
 
unsigned char rbuf_empty (rbuf_handle_t rbuf)
 Checks if the buffer is empty or not. More...
 
unsigned char rbug_full (rbuf_handle_t rbuf)
 Checks if the buffer is full. More...
 
size_t rbuf_size (rbuf_handle_t rbuf)
 Returns the number of elements currently stored in the ring buffer. More...
 
size_t rbuf_capacity (rbuf_handle_t rbuf)
 Returns the maximum number of elements that the ring buffer can store. More...
 

Variables

static pthread_mutex_t GL_thread_lock = PTHREAD_MUTEX_INITIALIZER
 
static pid_t cur_GL_thread = 0
 

Detailed Description

Utilities required by other TUM_XXX files.

Author
Alex Hoffman
Date
27 August 2019
   ----------------------------------------------------------------------
    Copyright (C) Alexander Hoffman, 2019
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   ----------------------------------------------------------------------

Function Documentation

◆ rbuf_capacity()

size_t rbuf_capacity ( rbuf_handle_t  rbuf)

Returns the maximum number of elements that the ring buffer can store.

Parameters
rbufHandle to the ring buffer
Returns
The maximum number of elements that the ring buffer can store

◆ rbuf_empty()

unsigned char rbuf_empty ( rbuf_handle_t  rbuf)

Checks if the buffer is empty or not.

Parameters
rbufHandle to the ring buffer
Returns
1 if the buffer is empty, 0 otherwise

◆ rbuf_fput()

int rbuf_fput ( rbuf_handle_t  rbuf,
void *  data 
)

Fills the next available buffer, overwriting data if the ring buffer is full.

Parameters
rbufHandle to the ring buffer
dataReference to the data to be copied into the buffer
Returns
0 on success

◆ rbuf_free()

void rbuf_free ( rbuf_handle_t  rbuf)

Frees a ring buffer.

Parameters
rbufHandle to the ring buffer

◆ rbuf_get()

int rbuf_get ( rbuf_handle_t  rbuf,
void *  data 
)

Returns a copy of the next buffer item's data.

Parameters
rbufHandle to the ring buffer
dataA reference to the allocated memory region into which the data should be copied
Returns
0 on success

◆ rbuf_get_buffer()

void* rbuf_get_buffer ( rbuf_handle_t  rbuf)

Returns a reference to the data of the next ring buffer entry.

Because only a reference is returned the contents of the buffer entry cannot be guarenteed

Parameters
rbufHandle to the ring buffer
Returns
A reference to the next item in the buffer's data

◆ rbuf_init()

rbuf_handle_t rbuf_init ( size_t  item_size,
size_t  item_count 
)

Initialized a ring buffer object with a certain number of objects of a given size.

Parameters
item_sizeThe size, in bytes, of each ring buffer item
item_countThe maximum number of items to be stored in the ring buffer
Returns
A handle to the created ring buffer, else NULL

◆ rbuf_init_static()

rbuf_handle_t rbuf_init_static ( size_t  item_size,
size_t  item_count,
void *  buffer 
)

Initialized a ring buffer object with a certain number of objects of a given size into a statically allocated buffer.

Parameters
item_sizeThe size, in bytes, of each ring buffer item
item_countThe maximum number of items to be stored in the ring buffer
bufferReference to the statically allocated memory region that is to be used for storing the ring buffer
Returns
A handle to the created ring buffer, else NULL

◆ rbuf_put()

int rbuf_put ( rbuf_handle_t  rbuf,
void *  data 
)

Fills the next available buffer slot, if a slot is free.

Parameters
rbufHandle to the ring buffer
dataReference to the data to be copied into the buffer
Returns
0 on success

◆ rbuf_put_buffer()

int rbuf_put_buffer ( rbuf_handle_t  rbuf)

Used when a reference to the next buffer item is already filled, incrementing the next buffer item should an item need to be retrieved.

Parameters
rbufHandle to the ring buffer
Returns
0 on success

◆ rbuf_reset()

void rbuf_reset ( rbuf_handle_t  rbuf)

Resets the ring buffer to it's initial state.

Parameters
rbufHandle to the ring buffer

◆ rbuf_size()

size_t rbuf_size ( rbuf_handle_t  rbuf)

Returns the number of elements currently stored in the ring buffer.

Parameters
rbufHandle to the ring buffer
Returns
Number of elements stored in the ring buffer

◆ rbug_full()

unsigned char rbug_full ( rbuf_handle_t  rbuf)

Checks if the buffer is full.

Parameters
rbufHandle to the ring buffer
Returns
1 if the buffer is full, 0 otherwise

◆ recurseDirName()

static char* recurseDirName ( char *  dir_name,
char *  filename,
char  flags 
)
static

Finds and returns the full path of a file searched for in a target directory and its sub-directories.

The found filename is stored in a statically allocated buffer and can be overwritten by subsequent calls to the functions

Parameters
dir_nameTarget root directory to be searched
filenameThe file that is to be searched for
Returns
A reference to the statically allocated buffer where the filename is being stored, else NULL

◆ tumUtilFindResource()

FILE* tumUtilFindResource ( char *  resource_name,
const char *  mode 
)

Searches for a file in the RESOURCES_DIRECTORY and returns a FILE * if found.

Parameters
resource_nameName of the file to be found
modeThe reading mode to be used when opening the file, eg. "rw"
Returns
FILE reference if found, otherwise NULL

◆ tumUtilFindResourcePath()

char* tumUtilFindResourcePath ( char *  resource_name)

Similar to tumUtilFindResource() only returning the file's path instead of the opened FILE's reference.

The found filename is stored in a statically allocated buffer and can be overwritten by subsequent calls to the functions

Parameters
resource_nameName of the file to be found
Returns
Reference to the statically allocated filename, else NULL

◆ tumUtilGetBinFolderPath()

char* tumUtilGetBinFolderPath ( char *  bin_path)

Gets the execution folder of the current program, assumes that program is executing from a folder "bin".

Parameters
bin_pathThe program's binary's location, usually argv[0]
Returns
char * String of the folder's absolute location

◆ tumUtilIsCurGLThread()

int tumUtilIsCurGLThread ( void  )

Checks if the calling thread is the thread that currently holds the GL context.

Returns
0 if the current thread does hold the GL context, -1 otherwise.

◆ tumUtilPrependPath()

char* tumUtilPrependPath ( char *  path,
char *  file 
)

Prepends a path string to a filename.

Parameters
pathPath string to be prepended
fileFilename to which the path string should be prepended
Returns
char * to the complete compiled path