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:
parent
57cbd2ca08
commit
20dca2d2e4
@ -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;
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef _XmdBuffer_h
|
#ifndef _XmdBuffer_h
|
||||||
#define _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
|
/* 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
|
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;
|
typedef struct _XmdBuffer XmdBuffer;
|
||||||
|
|
||||||
/* XmdBufferNew creates a new buffer with the specified element size. */
|
/* 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. */
|
/* XmdBufferPush adds a new element to the buffer. */
|
||||||
void XmdBufferPush (XmdBuffer *buffer, void *element);
|
void XmdBufferPush (XmdBuffer *buffer, void *element);
|
||||||
|
@ -1,30 +1,32 @@
|
|||||||
#include <Xmd/Buffer.h>
|
#include <Xmd/Buffer.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define XmdBUFFER_GROWTH_FACTOR 2
|
#define XmdBUFFER_GROWTH_FACTOR 2
|
||||||
|
#define XmdBUFFER_INITIAL_CAPACITY 8
|
||||||
|
|
||||||
struct _XmdBuffer {
|
struct _XmdBuffer {
|
||||||
void * data;
|
void * data;
|
||||||
size_t element;
|
Cardinal size;
|
||||||
size_t length;
|
Cardinal length;
|
||||||
size_t capacity;
|
Cardinal capacity;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void XmdBufferResize (XmdBuffer *buffer, size_t length);
|
static void XmdBufferResize (XmdBuffer *buffer, Cardinal length);
|
||||||
static void XmdBufferFit (XmdBuffer *buffer);
|
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 *XmdBufferNew (Cardinal size) {
|
||||||
XmdBuffer *buffer = calloc(1, sizeof(XmdBuffer));
|
XmdBuffer *buffer = XtNew(XmdBuffer);
|
||||||
buffer->element = element;
|
buffer->size = size;
|
||||||
buffer->capacity = 1;
|
buffer->length = 0;
|
||||||
buffer->data = calloc(buffer->capacity, buffer->element);
|
buffer->capacity = XmdBUFFER_INITIAL_CAPACITY;
|
||||||
|
buffer->data = XtCalloc(buffer->capacity, buffer->size);
|
||||||
XmdBufferFit(buffer);
|
XmdBufferFit(buffer);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void XmdBufferResize (XmdBuffer *buffer, size_t length) {
|
void XmdBufferResize (XmdBuffer *buffer, Cardinal length) {
|
||||||
buffer->length = length;
|
buffer->length = length;
|
||||||
XmdBufferFit(buffer);
|
XmdBufferFit(buffer);
|
||||||
}
|
}
|
||||||
@ -32,14 +34,15 @@ void XmdBufferResize (XmdBuffer *buffer, size_t length) {
|
|||||||
void XmdBufferFit (XmdBuffer *buffer) {
|
void XmdBufferFit (XmdBuffer *buffer) {
|
||||||
if (buffer->length > buffer->capacity) {
|
if (buffer->length > buffer->capacity) {
|
||||||
buffer->capacity *= XmdBUFFER_GROWTH_FACTOR;
|
buffer->capacity *= XmdBUFFER_GROWTH_FACTOR;
|
||||||
buffer->data = realloc (
|
buffer->data = XtReallocArray (
|
||||||
buffer->data,
|
buffer->data,
|
||||||
buffer->capacity * buffer->element);
|
buffer->capacity,
|
||||||
|
buffer->size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void *XmdBufferOffset (XmdBuffer *buffer, size_t index) {
|
void *XmdBufferOffset (XmdBuffer *buffer, Cardinal index) {
|
||||||
return buffer->data + (index * buffer->element);
|
return buffer->data + (index * buffer->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void XmdBufferPush (XmdBuffer *buffer, void *element) {
|
void XmdBufferPush (XmdBuffer *buffer, void *element) {
|
||||||
@ -47,16 +50,16 @@ void XmdBufferPush (XmdBuffer *buffer, void *element) {
|
|||||||
memcpy (
|
memcpy (
|
||||||
XmdBufferOffset(buffer, buffer->length - 1),
|
XmdBufferOffset(buffer, buffer->length - 1),
|
||||||
element,
|
element,
|
||||||
buffer->element);
|
buffer->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *XmdBufferBreak (XmdBuffer *buffer) {
|
void *XmdBufferBreak (XmdBuffer *buffer) {
|
||||||
void *data = buffer->data;
|
void *data = buffer->data;
|
||||||
free(buffer);
|
XtFree((char *)(buffer));
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void XmdBufferFree (XmdBuffer *buffer) {
|
void XmdBufferFree (XmdBuffer *buffer) {
|
||||||
free(buffer->data);
|
XtFree(buffer->data);
|
||||||
free(buffer);
|
XtFree((char *)(buffer));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user