TPCCLIB
Loading...
Searching...
No Matches
mtac.c File Reference

Processing list of TAC structures. More...

#include "tpcclibConfig.h"
#include "tpcift.h"
#include "tpcisotope.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpctac.h"

Go to the source code of this file.

Functions

void mtacInit (MTAC *mtac)
 
void mtacFree (MTAC *mtac)
 
int mtacAllocate (MTAC *mtac, int nr)
 
int mtacAllocateMore (MTAC *mtac, int nr)
 
int mtacAddTAC (MTAC *mtac, TAC *tac)
 

Detailed Description

Processing list of TAC structures.

Definition in file mtac.c.

Function Documentation

◆ mtacAddTAC()

int mtacAddTAC ( MTAC * mtac,
TAC * tac )

Copy given TAC structure into MTAC list.

See also
mtacInit, mtacFree, mtacAllocate, MTAC, TAC
Returns
Returns TPCERROR status.
Parameters
mtacPointer to initiated MTAC structure; any old contents are preserved.
tacPointer to TAC structure to add.

Definition at line 121 of file mtac.c.

126 {
127 if(mtac==NULL || tac==NULL) return TPCERROR_FAIL;
128 /* Make room for the TAC */
129 int ret=mtacAllocateMore(mtac, 1); if(ret!=TPCERROR_OK) return(ret);
130 /* Copy TAC into the end of the list */
131 ret=tacDuplicate(tac, &mtac->tac[mtac->nr]); if(ret!=TPCERROR_OK) return(ret);
132 mtac->nr++;
133 return TPCERROR_OK;
134}
int mtacAllocateMore(MTAC *mtac, int nr)
Definition mtac.c:83
TAC * tac
Definition tpctac.h:151
int nr
Definition tpctac.h:153
int tacDuplicate(TAC *tac1, TAC *tac2)
Make a duplicate of TAC structure.
Definition tac.c:356
@ TPCERROR_FAIL
General error.
@ TPCERROR_OK
No error.

◆ mtacAllocate()

int mtacAllocate ( MTAC * mtac,
int nr )

Allocate memory for TACs in MTAC data structure.

See also
mtacInit, mtacFree, mtacAddTAC
Returns
Returns TPCERROR status.
Parameters
mtacPointer to initiated MTAC structure; any old contents are deleted. Number of TACs is kept at zero, but TACs in the list are initiated.
nrNr of TACs to allocate

Definition at line 56 of file mtac.c.

62 {
63 if(mtac==NULL) return TPCERROR_FAIL;
64 /* Delete any previous contents */
65 mtacFree(mtac);
66 /* If no memory is requested, then just return */
67 if(nr<1) return TPCERROR_OK;
68 /* Allocate the list of TACs */
69 mtac->tac=(TAC*)malloc(nr*sizeof(TAC));
70 if(mtac->tac==NULL) return TPCERROR_OUT_OF_MEMORY;
71 for(int i=0; i<nr; i++) tacInit(&mtac->tac[i]);
72 mtac->_nr=nr;
73 return TPCERROR_OK;
74}
void mtacFree(MTAC *mtac)
Definition mtac.c:38
int _nr
Definition tpctac.h:155
Definition tpctac.h:87
void tacInit(TAC *tac)
Definition tac.c:24
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.

Referenced by mtacAllocateMore().

◆ mtacAllocateMore()

int mtacAllocateMore ( MTAC * mtac,
int nr )

Allocate memory for more TACs in MTAC structure. Previous contents are not changed.

See also
mtacAllocate, mtacInit, mtacFree, mtacAddTAC, MTAC, TAC
Returns
Returns TPCERROR status.
Parameters
mtacPointer to initiated and previously allocated MTAC structure; any old contents are preserved, including nr of TACs.
nrNr of additional TAC structures to allocate; if structure contains unused space for requested TACs already, then nothing is done.

Definition at line 83 of file mtac.c.

90 {
91 if(mtac==NULL) return TPCERROR_FAIL;
92
93 /* If not allocated before, then use mtacAllocate */
94 if(mtac->_nr==0) return(mtacAllocate(mtac, nr));
95
96 /* Check if there is enough space already */
97 int newNr, addNr;
98 newNr=mtac->nr + nr;
99 addNr=newNr - mtac->_nr;
100 if(addNr<=0) return TPCERROR_OK;
101
102 /* Reallocate memory for TACC data */
103 TAC *tacPtr;
104 tacPtr=realloc(mtac->tac, sizeof(TAC)*newNr);
105 if(tacPtr==NULL) return TPCERROR_OUT_OF_MEMORY;
106 mtac->tac=tacPtr;
107
108 /* Update the nr of allocated TACs and initiate new TACs */
109 for(int i=mtac->_nr; i<newNr; i++) tacInit(&mtac->tac[i]);
110 mtac->_nr=newNr;
111
112 return TPCERROR_OK;
113}
int mtacAllocate(MTAC *mtac, int nr)
Definition mtac.c:56

Referenced by mtacAddTAC().

◆ mtacFree()

void mtacFree ( MTAC * mtac)

Free memory allocated for MTAC data. All contents are destroyed.

Precondition
Before first use initialize the MTAC struct with mtacInit().
See also
mtacInit, tacFree
Parameters
mtacPointer to MTAC.

Definition at line 38 of file mtac.c.

41 {
42 if(mtac==NULL) return;
43 /* Free allocated TACs. */
44 for(int i=0; i<mtac->_nr; i++) tacFree(&mtac->tac[i]);
45 free(mtac->tac);
46 // then set everything to zero or NULL again
47 mtacInit(mtac);
48}
void mtacInit(MTAC *mtac)
Definition mtac.c:23
void tacFree(TAC *tac)
Definition tac.c:106

Referenced by mtacAllocate().

◆ mtacInit()

void mtacInit ( MTAC * mtac)

Initiate the MTAC structure before any use.

See also
mtacFree, MTAC, TAC
Parameters
mtacPointer to MTAC.

Definition at line 23 of file mtac.c.

26 {
27 if(mtac==NULL) return;
28 mtac->nr=mtac->_nr=0;
29 mtac->tac=NULL;
30}

Referenced by mtacFree().