Guest User

Untitled

a guest
May 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <limits.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <pthread.h>
  6. #include <assert.h>
  7.  
  8. #define VECTOR_COL 10000000
  9. #define VECTOR_ROW 10
  10.  
  11. uint32_t position_to_size(uint32_t count)
  12. {
  13. count+=1;
  14.  
  15. if (count < UCHAR_MAX)
  16. {
  17. return count*sizeof(uint8_t);
  18. }
  19. else if (count < USHRT_MAX)
  20. {
  21. return UCHAR_MAX * sizeof(uint8_t) +
  22. (count - UCHAR_MAX) * sizeof(uint16_t);
  23. }
  24. else if (count < UINT_MAX)
  25. {
  26. return UCHAR_MAX * sizeof(uint8_t) +
  27. (USHRT_MAX - UCHAR_MAX) * sizeof(uint16_t) +
  28. (count - UINT_MAX - USHRT_MAX - UCHAR_MAX) * sizeof(uint32_t);
  29. }
  30. return -1;
  31. }
  32.  
  33. void alloc_large_vector_slot(void* vector, uint32_t offset, int value)
  34. {
  35. if (value < UCHAR_MAX)
  36. *(int8_t*)(vector+offset) = value;
  37. else if (value < USHRT_MAX)
  38. *(int16_t*)(vector+offset) = value;
  39. else if (value < UINT_MAX)
  40. *(int32_t*)(vector+offset) = value;
  41. }
  42.  
  43. int main (void)
  44. {
  45. // Too big for stack, allocate elsewhere
  46. void* large_vector = malloc(position_to_size(VECTOR_COL*VECTOR_ROW));
  47. // We only ever count [0,VECTOR_COL*VECTOR_ROW-1]
  48. uint32_t i, j, count = 0;
  49.  
  50. for (i = 0; i < VECTOR_COL; i++)
  51. {
  52. for (j = 0; j < VECTOR_ROW; j++)
  53. {
  54. alloc_large_vector_slot(large_vector,
  55. position_to_size(i*VECTOR_ROW+j),
  56. i*j);
  57. }
  58. }
  59.  
  60. free(large_vector);
  61. return 0;
  62. }
Add Comment
Please, Sign In to add comment