common.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #define null 0
  22. #include <assert.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #define UNDEF -1
  31. #define BUF_LEN 256
  32. #define INFTY 1000000
  33. #define EPS 0.0001
  34. #ifndef MAX
  35. #define MAX(a,b) (((a)>(b))?(a):(b))
  36. #define MIN(a,b) (((a)<(b))?(a):(b))
  37. #endif
  38. #ifndef SQR
  39. #define SQR(x) ((x) * (x))
  40. #endif
  41. #define ERR_MESG(x) { perror(x); return -1; }
  42. #if DEBUG
  43. //#ifdef __GNUC__
  44. #define dprintf(format, ...) fprintf(stderr, format, ## __VA_ARGS__)
  45. #else
  46. #define dprintf(format, ...)
  47. #endif
  48. #define Malloc(n,type) (type *) malloc( (unsigned) ((n)*sizeof(type)))
  49. #define Realloc(loc,n,type) (type *) realloc( (char *)(loc), \
  50. (unsigned) ((n)*sizeof(type)))
  51. #define matrix_alloc(mat,rows,cols,type) { \
  52. int ii; \
  53. type *temp; \
  54. \
  55. if (NULL == (temp = (type *) malloc(rows*cols*sizeof(type))) || \
  56. NULL == (mat = (type **) malloc(rows * sizeof(type *)))) \
  57. ERR_MESG("Out of memory"); \
  58. for (ii = 0; ii < rows; temp += cols, ii++) \
  59. mat[ii] = temp; \
  60. }
  61. #define matrix_free(mat) { \
  62. free(mat[0]); \
  63. free(mat); \
  64. }
  65. #endif