/*
- * Handle library memory errors ourselves rather than accept a default
+ * Handle library errors ourselves rather than accept a default
* fprintf to stderr (since we've mucked with the termios struct) */
static void library_err (const char *fmts, ...) NORETURN;
static void library_err (const char *fmts, ...) {
if (*glob) PUTT("%.*s", Screen_cols -1, glob);
} // end: show_special
\f
-/*###### Low Level Keyboard support ####################################*/
+/*###### Low Level Memory/Keyboard support #############################*/
+
+ /*
+ * Handle our own memory stuff without the risk of leaving the
+ * user's terminal in an ugly state should things go sour. */
+
+static void *alloc_c (size_t num) MALLOC;
+static void *alloc_c (size_t num) {
+ void *pv;
+
+ if (!num) ++num;
+ if (!(pv = calloc(1, num)))
+ error_exit("failed memory allocate");
+ return pv;
+} // end: alloc_c
+
+
+static void *alloc_r (void *ptr, size_t num) MALLOC;
+static void *alloc_r (void *ptr, size_t num) {
+ void *pv;
+
+ if (!num) ++num;
+ if (!(pv = realloc(ptr, num)))
+ error_exit("failed memory re-allocate");
+ return pv;
+} // end: alloc_r
+
/*
* This routine isolates ALL user INPUT and ensures that we
// we'll only grow our Pseudo_screen, never shrink it
if (pseudo_max < Pseudo_size) {
pseudo_max = Pseudo_size;
- Pseudo_screen = realloc(Pseudo_screen, pseudo_max);
+ Pseudo_screen = alloc_r(Pseudo_screen, pseudo_max);
}
PSU_CLREOS(0);
if (Frames_resize) putp(Cap_clr_scr);
/* note: we allocate one more CPU_t than Cpu_tot so that the last slot
can hold tics representing the /proc/stat cpu summary (the first
line read) -- that slot supports our View_CPUSUM toggle */
- if ((cpus = calloc((1 + Cpu_tot),sizeof(CPU_t)))==NULL)
- error_exit(fmtmk("failed to allocate memory for CPU_t"));
+ cpus = alloc_c((1 + Cpu_tot) * sizeof(CPU_t));
}
rewind(fp);
fflush(fp);
if (Frame_maxtask+1 >= HHist_siz) {
HHist_siz = HHist_siz * 5 / 4 + 100;
- PHist_sav = realloc(PHist_sav, sizeof(HST_t) * HHist_siz);
- PHist_new = realloc(PHist_new, sizeof(HST_t) * HHist_siz);
+ PHist_sav = alloc_r(PHist_sav, sizeof(HST_t) * HHist_siz);
+ PHist_new = alloc_r(PHist_new, sizeof(HST_t) * HHist_siz);
}
/* calculate time in this process; the sum of user time (utime) and
for (;;) {
if (n_used == n_alloc) {
n_alloc = 10 + ((n_alloc * 5) / 4); // grow by over 25%
- private_ppt = realloc(private_ppt, sizeof(proc_t*) * n_alloc);
+ private_ppt = alloc_r(private_ppt, sizeof(proc_t*) * n_alloc);
// ensure NULL pointers for the additional memory just acquired
memset(private_ppt + n_used, 0, sizeof(proc_t*) * (n_alloc - n_used));
}
else {
n_saved = n_alloc;
for (i = 0; i < GROUPSMAX; i++) {
- Winstk[i].ppt = realloc(Winstk[i].ppt, sizeof(proc_t*) * n_alloc);
+ Winstk[i].ppt = alloc_r(Winstk[i].ppt, sizeof(proc_t*) * n_alloc);
memcpy(Winstk[i].ppt, private_ppt, sizeof(proc_t*) * n_used);
}
}
struct sigaction sa;
int i;
- // setup our program name and library error message handler -- big!
+ // setup our program name -- big!
Myname = strrchr(me, '/');
if (Myname) ++Myname; else Myname = me;
qsort(Seed_ppt, Frame_maxtask, sizeof(proc_t*), Fieldstab[P_PPD].sort);
if (hwmsav < Frame_maxtask) { // grow, but never shrink
hwmsav = Frame_maxtask;
- Tree_ppt = realloc(Tree_ppt, sizeof(proc_t*) * hwmsav);
+ Tree_ppt = alloc_r(Tree_ppt, sizeof(proc_t*) * hwmsav);
}
while (0 == Seed_ppt[i]->ppid) // identify trees (expect 2)
forest_add(i++, 1); // add parent plus children