main() { int i,j,*ipvt,info,n=N; double **a,*b,*z,rcond; a = (double**)alloc2(n,n,sizeof(double)); ipvt = (int*)alloc1(n,sizeof(int)); b = (double*)alloc1(n,sizeof(double)); z = (double*)alloc1(n,sizeof(double)); /* Examples 3.6 & 3.7 from Kahaner, Moler, and Nash */ a[0][0] = 9.7; a[1][0] = 6.6; a[0][1] = 4.1; a[1][1] = 2.8; b[0] = 9.70; b[1] = 4.11; /* dgefa(a,n,ipvt,&info); printf("info = %d\n",info); */ dgeco(a,n,ipvt,&rcond,z); printf("rcond = %g\n",rcond); printf("condition number estimate = %g\n",1.0/rcond); printf("number of significant figures = %d\n", (int)log10(rcond/DBL_EPSILON)); dgesl(a,n,ipvt,b,0); pvecd(n,b); }
void wrapArray(void *base, size_t nmemb, size_t size, int f) /****************************************************************** wrapArray - wrap an array ******************************************************************* Author: Potash Corporation: Balasz Nemeth, given to CWP 2008 ******************************************************************/ { char *base2; char *base1; int shift; shift = f%(int)nmemb; if(shift==0) return; base2 = alloc1(nmemb,size); base1 = (char *) base; if(shift >0) shift=-nmemb+shift; memcpy((void *) base2, (const void *) (base1-shift*size),(nmemb+shift)*size); memcpy((void *) (base2+(nmemb+shift)*size), (const void *) base1,-shift*size); memcpy((void *) base1, (const void *) base2,nmemb*size); free1(base2); }
struct node_top* create_sll(void) { struct node_top *sll = alloc1(); struct node_top *now = sll; // NOTE: running this on bare metal may cause the machine to swap a bit int i; for (i = 1; i; ++i) { now->next = alloc2(); now = now->next; now->next = alloc1(); now = now->next; } return sll; }
int main() { typedef rt::node_allocator_lazy<int> inner_alloc_type; typedef std::list<int, inner_alloc_type> inner_list_type; typedef std::scoped_allocator_adaptor< rt::node_allocator_lazy<inner_list_type> , inner_alloc_type> outer_alloc_type; typedef std::list<inner_list_type, outer_alloc_type> outer_list_type; std::array<char, 2000> buffer1 = {{}}; std::array<char, 2000> buffer2 = {{}}; rt::node_alloc_header header1(buffer1); rt::node_alloc_header header2(buffer2); inner_alloc_type alloc1(&header1); rt::node_allocator_lazy<inner_list_type> alloc2(&header2); outer_alloc_type alloc(alloc2, alloc1); outer_list_type t1(alloc); t1.push_back({{5, 3, 7, 20}, alloc1}); t1.push_back({{1, 44, 22, 8}, alloc1}); rt::print(t1.front()); rt::print(t1.back()); }
/* allocate a 1-d array */ void *ealloc1 (size_t n1, size_t size) { void *p; if (ERROR == (p=alloc1(n1, size))) syssuerr("%s: malloc failed", __FILE__); return p; }
void allocator() { aa_mem_region_t reg; aa_mem_region_init( ®, 1024 ); /*--- List ---*/ amino::RegionList<int>::allocator alloc(®); amino::RegionList<int>::type list(alloc); list.push_back(1); list.push_back(2); list.push_back(3); printf("List:\n"); for( amino::RegionList<int>::iterator p = list.begin(); p != list.end(); p++ ) { printf("> %d\n", *p); } /*--- Vector ---*/ amino::RegionVector<int>::allocator alloc1(®); amino::RegionVector<int>::type vector(3, 0, alloc1); vector[0] = 10; vector[1] = 20; vector[2] = 30; printf("Vector:\n"); for( amino::RegionVector<int>::iterator p = vector.begin(); p != vector.end(); p++ ) { printf("> %d\n", *p); } /*--- Map ---*/ amino::RegionMap<int,int>::allocator alloc2(®); amino::RegionMap<int,int>::type map(std::less<int>(), alloc2); map[1] = 10; map[2] = 20; map[3] = 30; printf("Map:\n> %d %d %d\n", map[1], map[2], map[3] ); /*--- Stats ---*/ printf("Stats:\n" "> start: 0x%lx\n" "> head: 0x%lx\n" "> used: %lu\n", (intptr_t)reg.node->d, (intptr_t)reg.head, (intptr_t)reg.head - (intptr_t)reg.node->d ); aa_mem_region_destroy( ® ); }
int readlines(char *lineptr[], int maxlines) { int len, nlines; char *p, line[MAXLEN]; nlines = 0; while ((len = getline1(line, MAXLEN)) > 0) if (nlines >= maxlines || (p = alloc1(len)) == NULL) return -1; else { line[len-1] = '\0'; /* delete newline */ strcpy(p, line); lineptr[nlines++] = p; } return nlines; }
bool test_deallocate() { const std::size_t ptr_size = sizeof (char*); const std::size_t data_size = sizeof (std::size_t); std::array<char, 3 * data_size> buffer = {{}}; rt::node_alloc_header header(buffer); rt::node_allocator_lazy<int> alloc1(&header); // links the node_allocator_lazy. rt::node_allocator_lazy<std::size_t> alloc2(alloc1); std::size_t* p1 = alloc2.allocate_node(); *p1 = 10; // To avoid a warning. std::size_t* p2 = alloc2.allocate_node(); try { std::size_t* p3 = alloc2.allocate_node(); // Should throw. *p3 = 100; // To avoid a warning. return false; } catch (...) { alloc2.deallocate_node(p2); } return true; }
static void drawimage (Display *dpy, Drawable dbl, Region region, FGC fgc, Model *m, float fmin, float fmax, int style) { int scr=-1; int x,y,width,height; int i,j,k,line,iline,jline,widthpad; unsigned long pmin,pmax,p; float fx,fy,s,base,scale; Tri *t=NULL; TriAttributes *ta; XRectangle rect; XImage *image=NULL; int bitmap_pad=0; int nbpr=0; #if 0 /* OLD VERSION . See JG fix below */ unsigned char *data=NULL; scr=DefaultScreen(dpy); /* Kludge to fix problem with XCreateImage introduced in */ /* Xorg 7.0 update for security */ if (BitmapPad(dpy)>16) { bitmap_pad = 16; } else if (BitmapPad(dpy) < 16) { bitmap_pad = 8; } /* determine smallest box enclosing region */ XClipBox(region,&rect); x = rect.x; y = rect.y; width = rect.width; height = rect.height; if (width==0 || height==0) return; /* allocate memory for image data */ widthpad = (1+(width-1)/bitmap_pad)*bitmap_pad; nbpr = widthpad-1; data = alloc1(widthpad*height,sizeof(unsigned char)); if (data==NULL) err("width,widthpad,height = %d %d %d", width,widthpad,height); warn("nbpr = %d widthpad = %d height = %d bitmap_pad = %d ", nbpr,widthpad,height,bitmap_pad); /* determine min and max pixels from standard colormap */ pmin = XtcwpGetFirstPixel(dpy); pmax = XtcwpGetLastPixel(dpy); /* determine base and scale factor */ scale = (fmax!=fmin) ? ((float) (pmax-pmin))/(fmax-fmin) : 0.0; base = ((float) pmin)-fmin*scale; /* loop over scan lines */ for (line=0; line<height; line++) { iline = line*width; jline = line*widthpad; /* loop over pixels in scan line */ for (i=iline,j=jline,k=0; k<width; ++i,++j,++k) { /* determine float x and y coordinates */ if (style==XtcwpNORMAL) { fx = MapX(fgc,x+k); fy = MapY(fgc,y+line); } else { fx = MapY(fgc,y+line); fy = MapX(fgc,x+k); } /* determine sloth */ t = insideTriInModel(m,t,fx,fy); ta = (TriAttributes*)t->fa; s = ta->s00+fy*ta->dsdx+fx*ta->dsdz; /* convert to pixel and put in image */ p = (unsigned long) (base+s*scale); if (p<pmin) p = pmin; if (p>pmax) p = pmax; data[j] = (unsigned char) p; } for (j=jline+width,k=width; k<widthpad; ++j,++k) data[j] = data[jline+width-1]; } /* create, put, and destroy image */ image = XCreateImage( (Display *) dpy, (Visual *) DefaultVisual(dpy,scr), (unsigned int) DefaultDepth(dpy,scr), (int)ZPixmap, (int) 0, (char*)data, (unsigned int) widthpad, (unsigned int) height, (int) bitmap_pad, (int) nbpr); #else char *data=NULL; char noCmap; scr=DefaultScreen(dpy); /* JG: get bitmap_pad from X */ bitmap_pad = BitmapPad(dpy); /* determine smallest box enclosing region */ XClipBox(region,&rect); x = rect.x; y = rect.y; width = rect.width; height = rect.height; if (width==0 || height==0) return; /* allocate memory for image data */ widthpad = (1+(width-1)/bitmap_pad)*bitmap_pad; nbpr = widthpad-1; /* create image & determine alloc size from X */ image = XCreateImage( (Display *) dpy, (Visual *) DefaultVisual(dpy,scr), (unsigned int) DefaultDepth(dpy,scr), (int)ZPixmap, (int) 0, NULL , (unsigned int) widthpad, (unsigned int) height, (int) bitmap_pad, 0); /* JG XCreateImage(....,0) gets X to compute the size it needs. Then we alloc. */ image->data = (char *)calloc(image->bytes_per_line, image->height); if (image->data==NULL) err("width,widthpad,height = %d %d %d", width,widthpad,height); /* warn("nbpr = %d widthpad = %d height = %d bitmap_pad = %d ", nbpr,widthpad,height,bitmap_pad); */ /* determine min and max pixels from standard colormap */ pmin = XtcwpGetFirstPixel(dpy); pmax = XtcwpGetLastPixel(dpy); /* JGHACK ... When colormap fails, we get pmax=pmin=0 */ noCmap = (pmax==0 && pmin==0) ? 1:0; if (noCmap) { pmax = 255; warn("No colormap found...."); } /* ...JGHACK */ /* determine base and scale factor */ scale = (fmax!=fmin) ? ((float) (pmax-pmin))/(fmax-fmin) : 0.0; base = ((float) pmin)-fmin*scale; data = (char *)image->data ; /* loop over scan lines */ for (line=0; line<height; line++) { iline = line*width; jline = line*widthpad; /* loop over pixels in scan line */ for (i=iline,j=jline,k=0; k<width; ++i,++j,++k) { /* determine float x and y coordinates */ if (style==XtcwpNORMAL) { fx = MapX(fgc,x+k); fy = MapY(fgc,y+line); } else { fx = MapY(fgc,y+line); fy = MapX(fgc,x+k); } /* determine sloth */ t = insideTriInModel(m,t,fx,fy); ta = (TriAttributes*)t->fa; s = ta->s00+fy*ta->dsdx+fx*ta->dsdz; /* convert to pixel and put in image */ p = (unsigned long) (base+s*scale); if (p<pmin) p = pmin; if (p>pmax) p = pmax; if (noCmap) { /* JG. Can't get colormap. Might as well write RGB pixels as grayscale */ XPutPixel(image,k,line, p | (p<<8)| (p<<16)); } else { /* original */ /* data[j] = (unsigned char) p; */ XPutPixel(image,k,line,p); } } /* original. Not sure this is needed JG */ /* for (j=jline+width,k=width; k<widthpad; ++j,++k) data[j] = data[jline+width-1]; */ } #endif XPutImage(dpy,dbl,fgc->gc,image,0,0,x,y,image->width,image->height); /* free(data); */ XDestroyImage(image); }
/* allocate a 1-d array of complexs */ dcomplex *alloc1dcomplex(size_t n1) { return (dcomplex*)alloc1(n1,sizeof(dcomplex)); }
/* allocate a 1-d array of doubles */ double *alloc1double(size_t n1) { return (double*)alloc1(n1,sizeof(double)); }
/* allocate a 1-d array of floats */ float *alloc1float(size_t n1) { return (float*)alloc1(n1,sizeof(float)); }
/* allocate a 1-d array of ints */ int *alloc1int(size_t n1) { return (int*)alloc1(n1,sizeof(int)); }