26 "%s is being limited from [%d] to [%d]\n", name, cur, max);
37 size_t len = strlen(src);
44 memcpy(dest, src, len);
54 my_fopen(
const char *fname,
const char *flag,
int prompt) {
57 char *new_fname = NULL;
58 char prompt_filename[256];
62 if (strchr(flag,
'w')) {
66 new_fname = (
char *)
my_malloc(Len *
sizeof(
char));
68 strcat(new_fname, fname);
74 int check_num_of_entered_values = scanf(
"%s", prompt_filename);
75 while (getchar() !=
'\n')
78 while (check_num_of_entered_values != 1) {
80 "Was expecting one file name to be entered, with no spaces. You have entered %d parameters. Please try again: \n",
81 check_num_of_entered_values);
82 check_num_of_entered_values = scanf(
"%s", prompt_filename);
84 fname = prompt_filename;
87 if (NULL == (fp = fopen(fname, flag))) {
89 "Error opening file %s for %s access: %s.\n", fname, flag,
109 Len = 1 + strlen(str);
110 Dst = (
char *)
my_malloc(Len *
sizeof(
char));
111 memcpy(Dst, str, Len);
121 if (str[0] <
'0' || str[0] >
'9') {
122 if (!(str[0] ==
'-' && str[1] >=
'0' && str[1] <=
'9')) {
123 vpr_printf(TIO_MESSAGE_ERROR,
"expected number instead of '%s'.\n",
138 if ((ret = calloc(nelem, size)) == NULL ) {
140 "Error: Unable to calloc memory. Aborting.\n");
153 if ((ret = malloc(size)) == NULL ) {
155 "Error: Unable to malloc memory. Aborting.\n");
167 vpr_printf(TIO_MESSAGE_WARNING,
"reallocating of size <= 0.\n");
170 ret = realloc(ptr, size);
172 vpr_printf(TIO_MESSAGE_ERROR,
"Unable to realloc memory. Aborting. "
173 "ptr=%p, Size=%d.\n", ptr, (
int) size);
176 "my_realloc: ptr == NULL. Aborting.\n");
208 #define CHUNK_SIZE 32768
209 #define FRAGMENT_THRESHOLD 100
216 if ((
size_t) (chunk_info->
mem_avail) < size) {
228 assert(chunk_info != NULL);
237 assert(chunk_info != NULL);
248 assert(chunk_info != NULL);
258 if (size %
sizeof(Align) == 0) {
261 aligned_size = size +
sizeof(Align) - size %
sizeof(Align);
278 while (curr_ptr != NULL ) {
281 curr_ptr = curr_ptr->
next;
301 linked_vptr->
next = head;
302 return (linked_vptr);
313 linked_vptr = head->
next;
329 if (*free_list_head_ptr != NULL ) {
330 linked_int = *free_list_head_ptr;
331 *free_list_head_ptr = linked_int->
next;
336 linked_int->
data = data;
337 linked_int->
next = head;
348 linked_int = *int_list_head_ptr;
350 while (linked_int != NULL ) {
351 next_linked_int = linked_int->
next;
353 linked_int = next_linked_int;
356 *int_list_head_ptr = NULL;
370 list_head = *list_head_ptr;
372 if (num_items == 0) {
376 if (list_head != NULL ) {
378 "alloc_ivector_and_copy_int_list: Copied %d elements, "
379 "but list at %p contains more.\n", num_items,
386 ivec->
nelem = num_items;
387 list = (
int *)
my_malloc(num_items *
sizeof(
int));
389 linked_int = list_head;
391 for (i = 0; i < num_items - 1; i++) {
392 list[i] = linked_int->
data;
393 linked_int = linked_int->
next;
396 list[num_items - 1] = linked_int->
data;
398 if (linked_int->
next != NULL ) {
400 "Error in alloc_ivector_and_copy_int_list:\n Copied %d elements, "
401 "but list at %p contains more.\n", num_items,
406 linked_int->
next = *free_list_head_ptr;
407 *free_list_head_ptr = list_head;
408 *list_head_ptr = NULL;
426 for (i = 0; i < max_size - 1; i++) {
441 while ((ch = fgetc(fp)) !=
'\n' && !feof(fp))
446 if (ch ==
'\r' || ch ==
'\n') {
447 if (i != 0 && buf[i - 1] ==
'\\') {
464 "Error on line %d -- line is too long for input buffer.\n",
467 "All lines must be at most %d characters long.\n",
BUFSIZE - 2);
472 my_strtok(
char *ptr,
const char *tokens, FILE * fp,
char *buf) {
485 val = strtok(ptr, tokens);
487 if (val != NULL ||
cont == 0)
494 val = strtok(buf, tokens);
504 for (i = nrmin; i <= nrmax; i++)
505 if (ivec_vector[i].nelem != 0)
506 free(ivec_vector[i].list);
508 free(ivec_vector + nrmin);
512 int ncmin,
int ncmax) {
518 for (i = nrmin; i <= nrmax; i++) {
519 for (j = ncmin; j <= ncmax; j++) {
520 if (ivec_matrix[i][j].nelem != 0) {
521 free(ivec_matrix[i][j].list);
530 int ncmin,
int ncmax,
int ndmin,
int ndmax) {
536 for (i = nrmin; i <= nrmax; i++) {
537 for (j = ncmin; j <= ncmax; j++) {
538 for (k = ndmin; k <= ndmax; k++) {
539 if (ivec_matrix3[i][j][k].nelem != 0) {
540 free(ivec_matrix3[i][j][k].list);
546 free_matrix3(ivec_matrix3, nrmin, nrmax, ncmin, ncmax, ndmin,
551 alloc_matrix(
int nrmin,
int nrmax,
int ncmin,
int ncmax,
size_t elsize) {
561 cptr = (
char **)
my_malloc((nrmax - nrmin + 1) *
sizeof(
char *));
563 for (i = nrmin; i <= nrmax; i++) {
564 cptr[i] = (
char *)
my_malloc((ncmax - ncmin + 1) * elsize);
565 cptr[i] -= ncmin * elsize /
sizeof(char);
567 return ((
void **) cptr);
573 void free_matrix(
void *vptr,
int nrmin,
int nrmax,
int ncmin,
size_t elsize) {
577 cptr = (
char **) vptr;
579 for (i = nrmin; i <= nrmax; i++)
580 free(cptr[i] + ncmin * elsize /
sizeof(
char));
585 alloc_matrix3(
int nrmin,
int nrmax,
int ncmin,
int ncmax,
int ndmin,
int ndmax,
597 cptr = (
char ***)
my_malloc((nrmax - nrmin + 1) *
sizeof(
char **));
599 for (i = nrmin; i <= nrmax; i++) {
600 cptr[i] = (
char **)
my_malloc((ncmax - ncmin + 1) *
sizeof(
char *));
602 for (j = ncmin; j <= ncmax; j++) {
603 cptr[i][j] = (
char *)
my_malloc((ndmax - ndmin + 1) * elsize);
604 cptr[i][j] -= ndmin * elsize /
sizeof(char);
607 return ((
void ***) cptr);
611 alloc_matrix4(
int nrmin,
int nrmax,
int ncmin,
int ncmax,
int ndmin,
int ndmax,
612 int nemin,
int nemax,
size_t elsize) {
623 cptr = (
char ****)
my_malloc((nrmax - nrmin + 1) *
sizeof(
char ***));
625 for (i = nrmin; i <= nrmax; i++) {
626 cptr[i] = (
char ***)
my_malloc((ncmax - ncmin + 1) *
sizeof(
char **));
628 for (j = ncmin; j <= ncmax; j++) {
630 (ndmax - ndmin + 1) *
sizeof(
char *));
632 for (k = ndmin; k <= ndmax; k++) {
634 (nemax - nemin + 1) * elsize);
635 cptr[i][j][k] -= nemin * elsize /
sizeof(char);
639 return ((
void ****) cptr);
643 int ndmin,
int ndmax,
char *file) {
649 for (k = nrmin; k <= nrmax; ++k) {
650 fprintf(outfile,
"Plane %d\n", k);
651 for (j = ncmin; j <= ncmax; ++j) {
652 for (i = ndmin; i <= ndmax; ++i) {
653 fprintf(outfile,
"%d ", vptr[k][j][i]);
655 fprintf(outfile,
"\n");
657 fprintf(outfile,
"\n");
663 void free_matrix3(
void *vptr,
int nrmin,
int nrmax,
int ncmin,
int ncmax,
664 int ndmin,
size_t elsize) {
668 cptr = (
char ***) vptr;
670 for (i = nrmin; i <= nrmax; i++) {
671 for (j = ncmin; j <= ncmax; j++)
672 free(cptr[i][j] + ndmin * elsize /
sizeof(
char));
673 free(cptr[i] + ncmin);
678 void free_matrix4(
void *vptr,
int nrmin,
int nrmax,
int ncmin,
int ncmax,
679 int ndmin,
int ndmax,
int nemin,
size_t elsize) {
683 cptr = (
char ****) vptr;
685 for (i = nrmin; i <= nrmax; i++) {
686 for (j = ncmin; j <= ncmax; j++) {
687 for (k = ndmin; k <= ndmax; k++)
688 free(cptr[i][j][k] + nemin * elsize /
sizeof(
char));
689 free(cptr[i][j] + ndmin * elsize /
sizeof(
char));
691 free(cptr[i] + ncmin);
699 #define IA 1103515245u
701 #define IM 2147483648u
719 ival = (int) ((
float) ival * (float) (imax + 0.999) / (float)
IM);
722 if ((ival < 0) || (ival > imax)) {
723 if (ival == imax + 1) {
728 "Bad value in my_irand, imax = %d ival = %d\n", imax,
747 fval = (float) ival / (
float)
IM;
750 if ((fval < 0) || (fval > 1.)) {
751 vpr_printf(TIO_MESSAGE_ERROR,
"Bad value in my_frand, fval = %g\n",
763 if (filename == NULL ) {
767 file = fopen(filename,
"r");
void free_matrix(void *vptr, int nrmin, int nrmax, int ncmin, size_t elsize)
void free_ivec_matrix3(struct s_ivec ***ivec_matrix3, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax)
int ipow(int base, int exp)
boolean file_exists(const char *filename)
int my_atoi(const char *str)
void * my_malloc(size_t size)
void * my_realloc(void *ptr, size_t size)
void ** alloc_matrix(int nrmin, int nrmax, int ncmin, int ncmax, size_t elsize)
struct s_linked_vptr * insert_in_vptr_list(struct s_linked_vptr *head, void *vptr_to_add)
void free_chunk_memory(t_chunk *chunk_info)
struct s_linked_vptr * chunk_ptr_head
void free_int_list(t_linked_int **int_list_head_ptr)
char * my_strtok(char *ptr, const char *tokens, FILE *fp, char *buf)
void alloc_ivector_and_copy_int_list(t_linked_int **list_head_ptr, int num_items, struct s_ivec *ivec, t_linked_int **free_list_head_ptr)
void * my_calloc(size_t nelem, size_t size)
void free_ivec_matrix(struct s_ivec **ivec_matrix, int nrmin, int nrmax, int ncmin, int ncmax)
char * my_strdup(const char *str)
void free_matrix4(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, size_t elsize)
int limit_value(int cur, int max, const char *name)
void free_matrix3(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, size_t elsize)
t_linked_int * insert_in_int_list(t_linked_int *head, int data, t_linked_int **free_list_head_ptr)
struct s_linked_vptr * next
FILE * my_fopen(const char *fname, const char *flag, int prompt)
struct s_linked_vptr * delete_in_vptr_list(struct s_linked_vptr *head)
char * my_strncpy(char *dest, const char *src, size_t size)
static unsigned int current_random
char * my_fgets(char *buf, int max_size, FILE *fp)
void **** alloc_matrix4(int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, int nemax, size_t elsize)
void my_srandom(int seed)
struct s_linked_int * next
void print_int_matrix3(int ***vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, char *file)
void *** alloc_matrix3(int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, size_t elsize)
unsigned char(* messagelogger)(TIO_MessageMode_t messageMode, char *pszMessage,...)
void free_ivec_vector(struct s_ivec *ivec_vector, int nrmin, int nrmax)
#define FRAGMENT_THRESHOLD
void * my_chunk_malloc(size_t size, t_chunk *chunk_info)