common.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2020, 2019, 2018, 2017 Girish M
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. #ifndef COMMON_H
  20. #define COMMON_H
  21. #include <assert.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <math.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #define UNDEF -1
  30. #define BUF_LEN 256
  31. #define INFTY 1000000
  32. #define EPS 0.0001
  33. #ifndef MAX
  34. #define MAX(a,b) (((a)>(b))?(a):(b))
  35. #define MIN(a,b) (((a)<(b))?(a):(b))
  36. #endif
  37. #ifndef SQR
  38. #define SQR(x) ((x) * (x))
  39. #endif
  40. #define ERR_MESG(x) { perror(x); return -1; }
  41. #if DEBUG
  42. //#ifdef __GNUC__
  43. #define dprintf(format, ...) fprintf(stderr, format, ## __VA_ARGS__)
  44. #else
  45. #define dprintf(format, ...)
  46. #endif
  47. #define Malloc(n,type) (type *) malloc( (unsigned) ((n)*sizeof(type)))
  48. #define Realloc(loc,n,type) (type *) realloc( (char *)(loc), \
  49. (unsigned) ((n)*sizeof(type)))
  50. #define matrix_alloc(mat,rows,cols,type) { \
  51. int ii; \
  52. type *temp; \
  53. \
  54. if (NULL == (temp = (type *) malloc(rows*cols*sizeof(type))) || \
  55. NULL == (mat = (type **) malloc(rows * sizeof(type *)))) \
  56. ERR_MESG("Out of memory"); \
  57. for (ii = 0; ii < rows; temp += cols, ii++) \
  58. mat[ii] = temp; \
  59. }
  60. #define matrix_free(mat) { \
  61. free(mat[0]); \
  62. free(mat); \
  63. }
  64. #endif