Team Ai
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes847downloads
AMem.txt181 linesDownload Raw Back to Adam
1 2#help_index "Memory/Task"3public I64 TaskMemAlloced(CTask *task=NULL,Bool override_validate=FALSE)4{//Count of bytes alloced to a task, used+unused.5  I64 res;6  if (!task) task=Fs;7  if (override_validate || TaskValidate(task)) {8    res=task->code_heap->alloced_u8s;9    if (task->code_heap!=task->data_heap)10      res+=task->data_heap->alloced_u8s;11    return res;12  } else13    return 0;14}15 16public I64 TaskMemUsed(CTask *task=NULL,Bool override_validate=FALSE)17{//Count of bytes alloced to a task and in use.18  I64 res;19  if (!task) task=Fs;20  if (override_validate || TaskValidate(task)) {21    res=task->code_heap->used_u8s;22    if (task->data_heap!=task->code_heap)23      res+=task->data_heap->used_u8s;24    return res;25  } else26    return 0;27}28 29#help_index "Memory/Task;Debugging/Heap;Memory/Debugging"30public Bool HeapRep(CTask *task)31{//Report status of task's heap.32  I64 i,cnt;33  CMemUnused *uum;34 35  if (!task || task==Fs) {36    "Task can't HeapRep on self.\n";37    return FALSE;38  }39  if (!TaskValidate(task)) return FALSE;40 41  PUSHFD42  CLI43  while (LBts(&task->code_heap->locked_flags,HClf_LOCKED))44    PAUSE45  if (task->data_heap!=task->code_heap)46    while (LBts(&task->data_heap->locked_flags,HClf_LOCKED))47      PAUSE48 49  for (i=0;i<MEM_HEAP_HASH_SIZE>>3;i++) {50    cnt=0;51    uum=task->code_heap->heap_hash[i];52    while (uum) {53      cnt+=uum->size;54      uum=uum->next;55    }56    if (task->data_heap!=task->code_heap) {57      uum=task->data_heap->heap_hash[i];58      while (uum) {59        cnt+=uum->size;60        uum=uum->next;61      }62    }63    if (cnt)64      "%03X:%08X\n",i<<3,cnt;65  }66  '\n';67 68  uum=task->code_heap->malloc_free_lst;69  while (uum) {70    "%X, ",uum->size;71    uum=uum->next;72  }73  if (task->data_heap!=task->code_heap) {74    uum=task->data_heap->malloc_free_lst;75    while (uum) {76      "%X, ",uum->size;77      uum=uum->next;78    }79  }80 81  if (task->data_heap!=task->code_heap)82    LBtr(&task->data_heap->locked_flags,HClf_LOCKED);83  LBtr(&task->code_heap->locked_flags,HClf_LOCKED);84  POPFD85 86  '\n';87}88 89#help_index "Memory/HeapCtrl;Debugging/Heap;Memory/Debugging"90public Bool IsInHeapCtrl(U8 *a,CHeapCtrl *hc,Bool lock=TRUE)91{//Check addr if in HeapCtrl.92  CMemBlk *m;93  PUSHFD94  CLI95  if (lock)96    while (LBts(&hc->locked_flags,HClf_LOCKED))97      PAUSE98  m=hc->next_mem_blk;99  while (m!=&hc->next_mem_blk) {100    if (a>=m && a<m(U8 *)+m->pags<<MEM_PAG_BITS) {101      if (lock)102        LBtr(&hc->locked_flags,HClf_LOCKED);103      POPFD104      return TRUE;105    }106    m=m->next;107  }108  if (lock)109    LBtr(&hc->locked_flags,HClf_LOCKED);110  POPFD111  return FALSE;112}113 114public Bool HeapCtrlWalk(CHeapCtrl *hc)115{//Check integrity of HeapCtrl.116  I64 i;117  CMemUnused *uum;118 119  PUSHFD120  CLI121  while (LBts(&hc->locked_flags,HClf_LOCKED))122    PAUSE123 124  for (i=0;i<MEM_HEAP_HASH_SIZE>>3;i++) {125    uum=hc->heap_hash[i];126    while (uum) {127      if (!IsInHeapCtrl(uum,hc,FALSE))128        goto hc_false;129      uum=uum->next;130    }131  }132  uum=hc->malloc_free_lst;133  while (uum) {134    if (!IsInHeapCtrl(uum,hc,FALSE))135      goto hc_false;136    uum=uum->next;137  }138 139  #if _CFG_HEAP_DBG140  CMemUsed *um,*um1;141  um1=(&hc->next_um)(U8 *)-offset(CMemUsed.next);142  um=um1->next;143  while (um!=um1) {144    if (!IsInHeapCtrl(um,hc,FALSE))145      goto hc_false;146    um=um->next;147  }148#endif149 150  LBtr(&hc->locked_flags,HClf_LOCKED);151  POPFD152  return TRUE;153 154  hc_false:155  LBtr(&hc->locked_flags,HClf_LOCKED);156  POPFD157  return FALSE;158}159 160#help_index "Memory/Task;Debugging/Heap;Memory/Debugging"161public Bool IsInHeap(U8 *a,CTask *task=NULL,Bool lock=TRUE)162{//Check addr if in task's heaps.163  if (!task) task=Fs;164  if (TaskValidate(task) && (IsInHeapCtrl(a,task->code_heap,lock)||165        task->data_heap!=task->code_heap &&166        IsInHeapCtrl(a,task->data_heap,lock)))167    return TRUE;168  else169    return FALSE;170}171 172public Bool HeapWalk(CTask *task=NULL)173{//Check integrity of task's heaps.174  if (!task) task=Fs;175  if (!TaskValidate(task) || !HeapCtrlWalk(task->code_heap) ||176        task->data_heap!=task->code_heap && !HeapCtrlWalk(task->data_heap))177    return FALSE;178  else179    return TRUE;180}181