Changeset 7844 in project


Ignore:
Timestamp:
01/22/08 13:33:34 (12 years ago)
Author:
felix winkelmann
Message:

applied Ivan Shmakov's mmap() patch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • chicken/trunk/runtime.c

    r7764 r7844  
    398398  *heap_scan_top,
    399399  *timer_start_fromspace_top;
     400static C_TLS size_t
     401  heapspace1_size,
     402  heapspace2_size;
    400403static C_TLS C_char
    401404  buffer[ STRING_BUFFER_SIZE ],
     
    10201023/* Align memory to page boundary */
    10211024
     1025#ifndef C_LOCK_TOSPACE
    10221026static void *align_to_page(void *mem)
    10231027{
     1028  return (void *)C_align((C_uword)mem);
     1029}
     1030#endif
     1031
     1032static C_byte *
     1033heap_alloc (size_t size, C_byte **page_aligned)
     1034{
     1035  C_byte *p;
    10241036#ifdef C_LOCK_TOSPACE
    1025   C_uword ptr = (C_word)mem;
    1026 
    1027   return (void *)(((ptr) + page_size - 1) & ~(page_size - 1));
     1037  p = (C_byte *)mmap (NULL, size, (PROT_READ | PROT_WRITE),
     1038                      (MAP_PRIVATE | MAP_ANONYMOUS), -1, 0);
     1039  if (p != NULL && page_aligned) *page_aligned = p;
    10281040#else
    1029   return (void *)C_align((C_uword)mem);
    1030 #endif
    1031 }
    1032 
     1041  p = (C_byte *)C_malloc (size + page_size);
     1042  if (p != NULL && page_aligned) *page_aligned = align_to_page (p);
     1043#endif
     1044
     1045  /* . */
     1046  return p;
     1047}
     1048
     1049static void
     1050heap_free (C_byte *ptr, size_t size)
     1051{
     1052#ifdef C_LOCK_TOSPACE
     1053  int r = munmap (ptr, size);
     1054  assert (r == 0);
     1055#else
     1056  C_free (ptr);
     1057#endif
     1058  /* . */
     1059}
     1060
     1061static C_byte *
     1062heap_realloc (C_byte *ptr, size_t old_size,
     1063              size_t new_size, C_byte **page_aligned)
     1064{
     1065  C_byte *p;
     1066#ifdef C_LOCK_TOSPACE
     1067  p = (C_byte *)mmap (NULL, new_size, (PROT_READ | PROT_WRITE),
     1068                      (MAP_PRIVATE | MAP_ANONYMOUS), -1, 0);
     1069  if (ptr != NULL) {
     1070    memcpy (p, ptr, old_size);
     1071    heap_free (ptr, old_size);
     1072  }
     1073  if (p != NULL && page_aligned) *page_aligned = p;
     1074#else
     1075  p = (C_byte *)C_realloc (ptr, new_size + page_size);
     1076  if (p != NULL && page_aligned) *page_aligned = align_to_page (p);
     1077#endif
     1078
     1079  /* . */
     1080  return p;
     1081}
    10331082
    10341083/* Modify heap size at runtime: */
     
    10361085void C_set_or_change_heap_size(C_word heap, int reintern)
    10371086{
    1038   C_byte *ptr1, *ptr2;
     1087  C_byte *ptr1, *ptr2, *ptr1a, *ptr2a;
    10391088  C_word size = heap / 2;
    10401089
     
    10471096  heap_size = heap;
    10481097
    1049   if((ptr1 = (C_byte *)C_realloc(fromspace_start, size + page_size)) == NULL ||
    1050      (ptr2 = (C_byte *)C_realloc(tospace_start, size + page_size)) == NULL)
     1098  if((ptr1 = heap_realloc (fromspace_start,
     1099                           C_fromspace_limit - fromspace_start,
     1100                           size, &ptr1a)) == NULL ||
     1101     (ptr2 = heap_realloc (tospace_start,
     1102                           tospace_limit - tospace_start,
     1103                           size, &ptr2a)) == NULL)
    10511104    panic(C_text("out of memory - can not allocate heap"));
    10521105
    1053   heapspace1 = ptr1;
    1054   heapspace2 = ptr2;
    1055   ptr1 = align_to_page(ptr1);
    1056   ptr2 = align_to_page(ptr2);
    1057   fromspace_start = ptr1;
     1106  heapspace1 = ptr1, heapspace1_size = size;
     1107  heapspace2 = ptr2, heapspace2_size = size;
     1108  fromspace_start = ptr1a;
    10581109  C_fromspace_top = fromspace_start;
    10591110  C_fromspace_limit = fromspace_start + size;
    1060   tospace_start = ptr2;
     1111  tospace_start = ptr2a;
    10611112  tospace_top = tospace_start;
    10621113  tospace_limit = tospace_start + size;
     
    31133164  FINALIZER_NODE *flist;
    31143165  TRACE_INFO *tinfo;
    3115   void *new_heapspace;
     3166  C_byte *new_heapspace;
     3167  size_t  new_heapspace_size;
    31163168
    31173169  lock_tospace(0);
     
    31393191  size /= 2;
    31403192
    3141   if((new_tospace_start = (C_byte *)C_malloc(size + page_size)) == NULL)
     3193  if ((new_heapspace = heap_alloc (size, &new_tospace_start)) == NULL)
    31423194    panic(C_text("out of memory - can not allocate heap segment"));
    3143 
    3144   new_heapspace = new_tospace_start;
    3145   new_tospace_start = align_to_page(new_tospace_start);
     3195  new_heapspace_size = size;
     3196
    31463197  new_tospace_top = new_tospace_start;
    31473198  new_tospace_limit = new_tospace_start + size;
     
    32303281  }
    32313282
    3232   C_free(heapspace1);
    3233   C_free(heapspace2);
     3283  heap_free (heapspace1, heapspace1_size);
     3284  heap_free (heapspace2, heapspace1_size);
    32343285 
    3235   if((tospace_start = (C_byte *)C_malloc(size + page_size)) == NULL)
     3286  if ((heapspace2 = heap_alloc (size, &tospace_start)) == NULL)
    32363287    panic(C_text("out ot memory - can not allocate heap segment"));
     3288  heapspace2_size = size;
    32373289
    32383290  heapspace1 = new_heapspace;
    3239   heapspace2 = tospace_start;
    3240   tospace_start = align_to_page(tospace_start);
     3291  heapspace1_size = new_heapspace_size;
    32413292  tospace_limit = tospace_start + size;
    32423293  tospace_top = tospace_start;
Note: See TracChangeset for help on using the changeset viewer.