MCPcopy Create free account
hub / github.com/GenericMappingTools/gmt / psl_memory

Function psl_memory

src/postscriptlight.c:585–624  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

583 */
584
585static void *psl_memory (struct PSL_CTRL *PSL, void *prev_addr, size_t nelem, size_t size) {
586 /* Multi-functional memory allocation subroutine.
587 If prev_addr is NULL, allocate new memory of nelem elements of size bytes.
588 Ignore when nelem == 0.
589 If prev_addr exists, reallocate the memory to a larger or smaller chunk of nelem elements of size bytes.
590 When nelem = 0, free the memory.
591 */
592
593 void *tmp = NULL;
594 const char *m_unit[4] = {"bytes", "kb", "Mb", "Gb"};
595 double mem;
596 int k;
597
598 if (prev_addr) {
599 if (nelem == 0) { /* Take care of n == 0 */
600 PSL_free (prev_addr);
601 return (NULL);
602 }
603 if ((tmp = realloc ( prev_addr, nelem * size)) == NULL) {
604 mem = (double)(nelem * size);
605 k = 0;
606 while (mem >= 1024.0 && k < 3) mem /= 1024.0, k++;
607 PSL_message (PSL, PSL_MSG_ERROR, "Error: Could not reallocate more memory [%.2f %s, %" PRIuS " items of %" PRIuS " bytes]\n",
608 mem, m_unit[k], nelem, size);
609 return (NULL);
610 }
611 }
612 else {
613 if (nelem == 0) return (NULL); /* Take care of n = 0 */
614 if ((tmp = calloc (nelem, size)) == NULL) {
615 mem = (double)(nelem * size);
616 k = 0;
617 while (mem >= 1024.0 && k < 3) mem /= 1024.0, k++;
618 PSL_message (PSL, PSL_MSG_ERROR, "Error: Could not allocate memory [%.2f %s, %" PRIuS " items of %" PRIuS " bytes]\n",
619 mem, m_unit[k], nelem, size);
620 return (NULL);
621 }
622 }
623 return (tmp);
624}
625
626/* Things to do with UTF8 */
627

Callers 2

psl_lzw_encodeFunction · 0.85
psl_makecolormapFunction · 0.85

Calls 1

PSL_messageFunction · 0.85

Tested by

no test coverage detected