rem: sprintf(RErrorString, "out of memory");
add: RErrorCode = RERR_NOMEMORY;
context: allocateGrayScale(RContext *ctx){    XColor *colors;    XColor avcolors[256];    int avncolors;    int i, ncolors, r, g, b;    int retries;    int cpc = ctx->attribs->colors_per_channel;    ncolors = cpc * cpc * cpc;        if (ctx->vclass == StaticGray) {      /* we might as well use all grays */      ncolors = 1<<ctx->depth;    } else {      if ( ncolors > (1<<ctx->depth) ) {	/* reduce colormap size */	cpc = ctx->attribs->colors_per_channel = 1<<((int)ctx->depth/3);	ncolors = cpc * cpc * cpc;      }            if (cpc < 2 || ncolors > (1<<ctx->depth)) {	sprintf(RErrorString, "invalid colormap size %i", cpc);	return NULL;      }    }    if (ncolors>=256 && ctx->vclass==StaticGray) {        /* don't need dithering for 256 levels of gray in StaticGray visual */        ctx->attribs->render_mode = RM_MATCH;    }    colors = malloc(sizeof(XColor)*ncolors);    if (!colors) {	sprintf(RErrorString, "out of memory");	return False;    }    for (i=0; i<ncolors; i++) {	colors[i].red=(i*0xffff) / (ncolors-1);	colors[i].green=(i*0xffff) / (ncolors-1);	colors[i].blue=(i*0xffff) / (ncolors-1);	colors[i].flags = DoRed|DoGreen|DoBlue;    }    /* try to allocate the colors */    for (i=0; i<ncolors; i++) {#ifdef DEBUG        printf("trying:%x,%x,%x\n",colors[i].red,colors[i].green,colors[i].blue);#endif	if (!XAllocColor(ctx->dpy, ctx->cmap, &(colors[i]))) {	    colors[i].flags = 0; /* failed */#ifdef DEBUG	    printf("failed:%x,%x,%x\n",colors[i].red,colors[i].green,colors[i].blue);#endif	} else {	    colors[i].flags = DoRed|DoGreen|DoBlue;	    #ifdef DEBUG	    printf("success:%x,%x,%x\n",colors[i].red,colors[i].green,colors[i].blue);#endif	}    }    /* try to allocate close values for the colors that couldn't      * be allocated before */    avncolors = (1<<ctx->depth>256 ? 256 : 1<<ctx->depth);    for (i=0; i<avncolors; i++) avcolors[i].pixel = i;    XQueryColors(ctx->dpy, ctx->cmap, avcolors, avncolors);    for (i=0; i<ncolors; i++) {	if (colors[i].flags==0) {	    int j;	    unsigned long cdiff=0xffffffff, diff;	    unsigned long closest=0;	    	    retries = 2;	    	    while (retries--) {		/* find closest color */		for (j=0; j<avncolors; j++) {		    r = (colors[i].red - avcolors[i].red)>>8;		    g = (colors[i].green - avcolors[i].green)>>8;		    b = (colors[i].blue - avcolors[i].blue)>>8;		    diff = r*r + g*g + b*b;		    if (diff<cdiff) {			cdiff = diff;			closest = j;		    }		}		/* allocate closest color found */#ifdef DEBUG		printf("best match:%x,%x,%x => %x,%x,%x\n",colors[i].red,colors[i].green,colors[i].blue,avcolors[closest].red,avcolors[closest].green,avcolors[closest].blue);#endif		colors[i].red = avcolors[closest].red;		colors[i].green = avcolors[closest].green;		colors[i].blue = avcolors[closest].blue;		if (XAllocColor(ctx->dpy, ctx->cmap, &colors[i])) {		    colors[i].flags = DoRed|DoGreen|DoBlue;		    break; /* succeeded, don't need to retry */		}#ifdef DEBUG		printf("close color allocation failed. Retrying...\n");#endif	    }	}    }    return colors;}