Use Xt memory allocation functions

Why do these even exist? I mean I guess there are some additions
compared to the standard ones but like why, why not just make
supplemental ones. Do these allocation functions do some wierd
different shit? What?
This commit is contained in:
Sasha Koshka 2023-11-06 03:28:08 -05:00
parent 57cbd2ca08
commit 20dca2d2e4
3 changed files with 27 additions and 61 deletions

View File

@ -1,37 +0,0 @@
#include <Xmd/Icon.h>
Pixmap _XmdLoadBitmapIcon (Widget widget, unsigned char *bits, int width, int height) {
Pixel fg, bg;
XtVaGetValues (widget,
XmNforeground, &fg,
XmNbackground, &bg,
NULL);
return XCreatePixmapFromBitmapData (
XtDisplay (widget),
RootWindowOfScreen(XtScreen(widget)),
(char *)(bits), width, height,
fg, bg, DefaultDepthOfScreen(XtScreen(widget)));
}
Pixmap XmdReadBitmapFile (Widget widget, const char *filename) {
Pixel fg, bg;
XtVaGetValues (widget,
XmNforeground, &fg,
XmNbackground, &bg,
NULL);
unsigned int width, height;
unsigned char *data;
int garbage;
XReadBitmapFileData (
filename,
&width, &height,
&data,
&garbage, &garbage);
Pixmap result = XCreatePixmapFromBitmapData (
XtDisplay (widget),
RootWindowOfScreen(XtScreen(widget)),
(char *)(data), width, height,
fg, bg, DefaultDepthOfScreen(XtScreen(widget)));
XFree(data);
return result;
}

View File

@ -1,7 +1,7 @@
#ifndef _XmdBuffer_h
#define _XmdBuffer_h
#include <stddef.h>
#include <X11/Intrinsic.h>
/* Buffer is a struct that keeps track of a variable length array of data. It
is like a piggy bank, you can add stuff to it and then break it to get access
@ -10,7 +10,7 @@
typedef struct _XmdBuffer XmdBuffer;
/* XmdBufferNew creates a new buffer with the specified element size. */
XmdBuffer *XmdBufferNew (size_t element);
XmdBuffer *XmdBufferNew (Cardinal size);
/* XmdBufferPush adds a new element to the buffer. */
void XmdBufferPush (XmdBuffer *buffer, void *element);

View File

@ -1,30 +1,32 @@
#include <Xmd/Buffer.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define XmdBUFFER_GROWTH_FACTOR 2
#define XmdBUFFER_GROWTH_FACTOR 2
#define XmdBUFFER_INITIAL_CAPACITY 8
struct _XmdBuffer {
void * data;
size_t element;
size_t length;
size_t capacity;
void * data;
Cardinal size;
Cardinal length;
Cardinal capacity;
};
static void XmdBufferResize (XmdBuffer *buffer, size_t length);
static void XmdBufferResize (XmdBuffer *buffer, Cardinal length);
static void XmdBufferFit (XmdBuffer *buffer);
static void *XmdBufferOffset (XmdBuffer *buffer, size_t index);
static void *XmdBufferOffset (XmdBuffer *buffer, Cardinal index);
XmdBuffer *XmdBufferNew (size_t element) {
XmdBuffer *buffer = calloc(1, sizeof(XmdBuffer));
buffer->element = element;
buffer->capacity = 1;
buffer->data = calloc(buffer->capacity, buffer->element);
XmdBuffer *XmdBufferNew (Cardinal size) {
XmdBuffer *buffer = XtNew(XmdBuffer);
buffer->size = size;
buffer->length = 0;
buffer->capacity = XmdBUFFER_INITIAL_CAPACITY;
buffer->data = XtCalloc(buffer->capacity, buffer->size);
XmdBufferFit(buffer);
return buffer;
}
void XmdBufferResize (XmdBuffer *buffer, size_t length) {
void XmdBufferResize (XmdBuffer *buffer, Cardinal length) {
buffer->length = length;
XmdBufferFit(buffer);
}
@ -32,14 +34,15 @@ void XmdBufferResize (XmdBuffer *buffer, size_t length) {
void XmdBufferFit (XmdBuffer *buffer) {
if (buffer->length > buffer->capacity) {
buffer->capacity *= XmdBUFFER_GROWTH_FACTOR;
buffer->data = realloc (
buffer->data = XtReallocArray (
buffer->data,
buffer->capacity * buffer->element);
buffer->capacity,
buffer->size);
}
}
void *XmdBufferOffset (XmdBuffer *buffer, size_t index) {
return buffer->data + (index * buffer->element);
void *XmdBufferOffset (XmdBuffer *buffer, Cardinal index) {
return buffer->data + (index * buffer->size);
}
void XmdBufferPush (XmdBuffer *buffer, void *element) {
@ -47,16 +50,16 @@ void XmdBufferPush (XmdBuffer *buffer, void *element) {
memcpy (
XmdBufferOffset(buffer, buffer->length - 1),
element,
buffer->element);
buffer->size);
}
void *XmdBufferBreak (XmdBuffer *buffer) {
void *data = buffer->data;
free(buffer);
XtFree((char *)(buffer));
return data;
}
void XmdBufferFree (XmdBuffer *buffer) {
free(buffer->data);
free(buffer);
XtFree(buffer->data);
XtFree((char *)(buffer));
}