1
0

std_mem.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef _STD_MEM_H
  2. #define _STD_MEM_H
  3. #include <bfc/platform/platform.h>
  4. #include <string.h>
  5. wchar_t *WMALLOC(size_t size);
  6. void *MALLOC(size_t size);
  7. void *CALLOC(size_t records, size_t recordsize);
  8. void *REALLOC(void *ptr, size_t size);
  9. void FREE(void *ptr);
  10. void *MEMDUP(const void *src, size_t n);
  11. void MEMCPY(void *dest, const void *src, size_t n);
  12. void MEMCPY_(void *dest, const void *src, size_t n);
  13. void MEMCPY32(void *dest, const void *src, size_t words);
  14. #ifdef __cplusplus
  15. static __inline int MEMCMP(const void *buf1, const void *buf2, size_t count) {
  16. return memcmp(buf1, buf2, count);
  17. }
  18. static __inline void MEMSET(void *dest, int c, size_t n) {
  19. memset(dest, c, n);
  20. }
  21. static __inline void MEMZERO(void *dest, size_t nbytes) {
  22. memset(dest, 0, nbytes);
  23. }
  24. #else
  25. #define MEMCMP memcmp
  26. #define MEMSET memset
  27. #define MEMZERO(dest, nbytes) memset(dest, 0, nbytes)
  28. #endif
  29. #ifdef __cplusplus
  30. // these are for structs and basic classes only
  31. static __inline void ZERO(int &obj) { obj = 0; }
  32. template<class T>
  33. inline void ZERO(T &obj) { MEMZERO(&obj, sizeof(T)); }
  34. // generic version that should work for all types
  35. template<class T>
  36. inline void MEMFILL(T *ptr, T val, unsigned int n) {
  37. for (int i = 0; i < n; i++) ptr[i] = val;
  38. }
  39. // asm 32-bits version
  40. void MEMFILL32(void *ptr, unsigned long val, unsigned int n);
  41. // helpers that call the asm version
  42. template<>
  43. inline void MEMFILL<unsigned long>(unsigned long *ptr, unsigned long val, unsigned int n) { MEMFILL32(ptr, val, n); }
  44. template<>
  45. void MEMFILL<unsigned short>(unsigned short *ptr, unsigned short val, unsigned int n);
  46. // int
  47. template<>
  48. inline void MEMFILL<int>(int *ptr, int val, unsigned int n) {
  49. MEMFILL32(ptr, *reinterpret_cast<unsigned long *>(&val), n);
  50. }
  51. // float
  52. template<>
  53. inline void MEMFILL<float>(float *ptr, float val, unsigned int n) {
  54. MEMFILL32(ptr, *reinterpret_cast<unsigned long *>(&val), n);
  55. }
  56. #endif // __cplusplus defined
  57. #endif