File: memory.c

package info (click to toggle)
vips 8.16.1-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 51,940 kB
  • sloc: ansic: 169,179; cpp: 11,890; python: 4,620; xml: 4,353; sh: 732; perl: 40; makefile: 19
file content (636 lines) | stat: -rw-r--r-- 14,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* tracked memory
 *
 * 2/11/99 JC
 *	- from im_open.c and callback.c
 *	- malloc tracking stuff added
 * 11/3/01 JC
 * 	- im_strncpy() added
 * 20/4/01 JC
 * 	- im_(v)snprintf() added
 * 6/7/05
 *	- more tracking for DEBUGM
 * 20/10/06
 * 	- return NULL for size <= 0
 * 11/5/06
 * 	- abort() on malloc() failure with DEBUG
 * 20/10/09
 * 	- gtkdoc comment
 * 6/11/09
 *	- im_malloc()/im_free() now call g_try_malloc()/g_free() ... removes
 *	  confusion over whether to use im_free() or g_free() for things like
 *	  im_header_string()
 * 21/9/11
 * 	- rename as vips_tracked_malloc() to emphasise difference from
 * 	  g_malloc()/g_free()
 */

/*

	This file is part of VIPS.

	VIPS is free software; you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
	02110-1301  USA

 */

/*

	These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif /*HAVE_IO_H*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#if defined(HAVE__ALIGNED_MALLOC) || defined(HAVE_MEMALIGN)
#include <malloc.h>
#endif

#include <vips/vips.h>
#include <vips/thread.h>

/**
 * SECTION: memory
 * @short_description: memory utilities
 * @stability: Stable
 * @include: vips/vips.h
 *
 * These functions cover two main areas.
 *
 * First, some simple utility functions over the underlying
 * g_malloc()/g_free() functions. Memory allocated and freeded using these
 * functions is interchangeable with any other glib library.
 *
 * Second, a pair of functions, vips_tracked_malloc() and vips_tracked_free(),
 * which are NOT compatible. If you g_free() memory that has been allocated
 * with vips_tracked_malloc() you will see crashes.
 *
 * The tracked functions are
 * only suitable for large allocations internal to the library, for example
 * pixel buffers. libvips watches the total amount of live tracked memory and
 * uses this information to decide when to trim caches.
 */

/* g_assert_not_reached() on memory errors.
#define DEBUG
 */

/* Track malloc/free and open/close.
#define DEBUG_VERBOSE_MEM
#define DEBUG_VERBOSE_FD
 */

#ifdef DEBUG
#warning DEBUG on in libsrc/iofuncs/memory.c
#endif /*DEBUG*/

static int vips_tracked_allocs = 0;
static size_t vips_tracked_mem = 0;
static int vips_tracked_files = 0;
static size_t vips_tracked_mem_highwater = 0;
static GMutex *vips_tracked_mutex = NULL;

/**
 * VIPS_NEW:
 * @OBJ: allocate memory local to @OBJ, or %NULL for no auto-free
 * @T: type of thing to allocate
 *
 * Allocate memory for a thing of type @T. The memory is not
 * cleared.
 *
 * This macro cannot fail. See vips_tracked_malloc() if you are
 * allocating large amounts of memory.
 *
 * See also: vips_malloc().
 *
 * Returns: A pointer of type @T *.
 */

/**
 * VIPS_ARRAY:
 * @OBJ: allocate memory local to @OBJ, or %NULL for no auto-free
 * @N: number of @T 's to allocate
 * @T: type of thing to allocate
 *
 * Allocate memory for an array of objects of type @T. The memory is not
 * cleared.
 *
 * This macro cannot fail. See vips_tracked_malloc() if you are
 * allocating large amounts of memory.
 *
 * See also: vips_malloc().
 *
 * Returns: A pointer of type @T *.
 */

static void
vips_malloc_cb(VipsObject *object, char *buf)
{
	g_free(buf);
}

/**
 * vips_malloc:
 * @object: (nullable): allocate memory local to this #VipsObject, or %NULL
 * @size: number of bytes to allocate
 *
 * g_malloc() local to @object, that is, the memory will be automatically
 * freed for you when the object is closed. If @object is %NULL, you need to
 * free the memory explicitly with g_free().
 *
 * This function cannot fail. See vips_tracked_malloc() if you are
 * allocating large amounts of memory.
 *
 * See also: vips_tracked_malloc().
 *
 * Returns: (transfer full): a pointer to the allocated memory.
 */
void *
vips_malloc(VipsObject *object, size_t size)
{
	void *buf;

	buf = g_malloc0(size);

	if (object) {
		g_signal_connect(object, "postclose",
			G_CALLBACK(vips_malloc_cb), buf);
		object->local_memory += size;
	}

	return buf;
}

/**
 * vips_strdup:
 * @object: (nullable): allocate memory local to this #VipsObject, or %NULL
 * @str: string to copy
 *
 * g_strdup() a string. When @object is freed, the string will be freed for
 * you.  If @object is %NULL, you need to
 * free the memory yourself with g_free().
 *
 * This function cannot fail.
 *
 * See also: vips_malloc().
 *
 * Returns: (transfer full): a pointer to the allocated memory
 */
char *
vips_strdup(VipsObject *object, const char *str)
{
	char *str_dup;

	str_dup = g_strdup(str);

	if (object) {
		g_signal_connect(object, "postclose",
			G_CALLBACK(vips_malloc_cb), str_dup);
		object->local_memory += strlen(str);
	}

	return str_dup;
}

/**
 * vips_tracked_free:
 * @s: (transfer full): memory to free
 *
 * Only use it to free memory that was
 * previously allocated with vips_tracked_malloc()
 * with a %NULL first argument.
 *
 * See also: vips_tracked_malloc().
 */
void
vips_tracked_free(void *s)
{
	/* Keep the size of the alloc in the previous 16 bytes. Ensures
	 * alignment rules are kept.
	 */
	void *start = (void *) ((char *) s - 16);
	size_t size = *((size_t *) start);

	g_mutex_lock(vips_tracked_mutex);

#ifdef DEBUG_VERBOSE_MEM
	printf("vips_tracked_free: %p, %zd bytes\n", s, size);
#endif /*DEBUG_VERBOSE_MEM*/

	if (vips_tracked_allocs <= 0)
		g_warning("%s", _("vips_free: too many frees"));
	if (vips_tracked_mem < size)
		g_warning("%s", _("vips_free: too much free"));

	vips_tracked_mem -= size;
	vips_tracked_allocs -= 1;

	g_mutex_unlock(vips_tracked_mutex);

	g_free(start);

	VIPS_GATE_FREE(size);
}

/**
 * vips_tracked_aligned_free:
 * @s: (transfer full): memory to free
 *
 * Only use it to free memory that was
 * previously allocated with vips_tracked_aligned_alloc()
 * with a %NULL first argument.
 *
 * See also: vips_tracked_aligned_alloc().
 */
void
vips_tracked_aligned_free(void *s)
{
	void *start = (size_t *) s - 1;
	size_t size = *((size_t *) start);

	g_mutex_lock(vips_tracked_mutex);

#ifdef DEBUG_VERBOSE
	printf("vips_tracked_aligned_free: %p, %zd bytes\n", s, size);
#endif /*DEBUG_VERBOSE*/

	if (vips_tracked_allocs <= 0)
		g_warning("%s", _("vips_free: too many frees"));
	if (vips_tracked_mem < size)
		g_warning("%s", _("vips_free: too much free"));

	vips_tracked_mem -= size;
	vips_tracked_allocs -= 1;

	g_mutex_unlock(vips_tracked_mutex);

#ifdef HAVE__ALIGNED_MALLOC
	_aligned_free(start);
#else /*defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)*/
	free(start);
#endif

	VIPS_GATE_FREE(size);
}

static void *
vips_tracked_init_mutex(void *data)
{
	vips_tracked_mutex = vips_g_mutex_new();

	return NULL;
}

static void
vips_tracked_init(void)
{
	static GOnce vips_tracked_once = G_ONCE_INIT;

	VIPS_ONCE(&vips_tracked_once,
		vips_tracked_init_mutex, NULL);
}

/**
 * vips_tracked_malloc:
 * @size: number of bytes to allocate
 *
 * Allocate an area of memory that will be tracked by vips_tracked_get_mem()
 * and friends.
 *
 * If allocation fails, vips_tracked_malloc() returns %NULL and
 * sets an error message.
 *
 * You must only free the memory returned with vips_tracked_free().
 *
 * See also: vips_tracked_free(), vips_malloc().
 *
 * Returns: (transfer full): a pointer to the allocated memory, or %NULL on error.
 */
void *
vips_tracked_malloc(size_t size)
{
	void *buf;

	vips_tracked_init();

	/* Need an extra sizeof(size_t) bytes to track
	 * size of this block. Ask for an extra 16 to make sure we don't break
	 * alignment rules.
	 */
	size += 16;

	if (!(buf = g_try_malloc0(size))) {
#ifdef DEBUG
		g_assert_not_reached();
#endif /*DEBUG*/

		vips_error("vips_tracked",
			_("out of memory --- size == %dMB"),
			(int) (size / (1024.0 * 1024.0)));
		g_warning(_("out of memory --- size == %dMB"),
			(int) (size / (1024.0 * 1024.0)));

		return NULL;
	}

	g_mutex_lock(vips_tracked_mutex);

	*((size_t *) buf) = size;
	buf = (void *) ((char *) buf + 16);

	vips_tracked_mem += size;
	if (vips_tracked_mem > vips_tracked_mem_highwater)
		vips_tracked_mem_highwater = vips_tracked_mem;
	vips_tracked_allocs += 1;

#ifdef DEBUG_VERBOSE_MEM
	printf("vips_tracked_malloc: %p, %zd bytes\n", buf, size);
#endif /*DEBUG_VERBOSE_MEM*/

	g_mutex_unlock(vips_tracked_mutex);

	VIPS_GATE_MALLOC(size);

	return buf;
}

/**
 * vips_tracked_aligned_alloc:
 * @size: number of bytes to allocate
 * @align: specifies the alignment
 *
 * Allocate an area of memory aligned on a boundary specified
 * by @align that will be tracked by vips_tracked_get_mem()
 * and friends.
 *
 * If allocation fails, vips_tracked_aligned_alloc() returns %NULL
 * and sets an error message.
 *
 * You must only free the memory returned with vips_tracked_aligned_free().
 *
 * See also: vips_tracked_malloc(), vips_tracked_aligned_free(), vips_malloc().
 *
 * Returns: (transfer full): a pointer to the allocated memory, or %NULL on error.
 */
void *
vips_tracked_aligned_alloc(size_t size, size_t align)
{
	void *buf;

	vips_tracked_init();

	g_assert(!(align & (align - 1)));

	/* Need an extra sizeof(size_t) bytes to track
	 * size of this block.
	 */
	size += sizeof(size_t);

#ifdef HAVE__ALIGNED_MALLOC
	if (!(buf = _aligned_malloc(size, align))) {
#elif defined(HAVE_POSIX_MEMALIGN)
	if (posix_memalign(&buf, align, size)) {
#elif defined(HAVE_MEMALIGN)
	if (!(buf = memalign(align, size))) {
#else
#error Missing aligned alloc implementation
#endif
#ifdef DEBUG
		g_assert_not_reached();
#endif /*DEBUG*/

		vips_error("vips_tracked",
			_("out of memory --- size == %dMB"),
			(int) (size / (1024.0 * 1024.0)));
		g_warning(_("out of memory --- size == %dMB"),
			(int) (size / (1024.0 * 1024.0)));

		return NULL;
	}

	memset(buf, 0, size);

	g_mutex_lock(vips_tracked_mutex);

	*((size_t *) buf) = size;

	vips_tracked_mem += size;
	if (vips_tracked_mem > vips_tracked_mem_highwater)
		vips_tracked_mem_highwater = vips_tracked_mem;
	vips_tracked_allocs += 1;

#ifdef DEBUG_VERBOSE
	printf("vips_tracked_aligned_alloc: %p, %zd bytes\n", buf, size);
#endif /*DEBUG_VERBOSE*/

	g_mutex_unlock(vips_tracked_mutex);

	VIPS_GATE_MALLOC(size);

	return (void *) ((size_t *) buf + 1);
}

/**
 * vips_tracked_open:
 * @pathname: name of file to open
 * @flags: flags for open()
 * @mode: open mode
 *
 * Exactly as open(2), but the number of files currently open via
 * vips_tracked_open() is available via vips_tracked_get_files(). This is used
 * by the vips operation cache to drop cache when the number of files
 * available is low.
 *
 * You must only close the file descriptor with vips_tracked_close().
 *
 * @pathname should be utf8.
 *
 * See also: vips_tracked_close(), vips_tracked_get_files().
 *
 * Returns: a file descriptor, or -1 on error.
 */
int
vips_tracked_open(const char *pathname, int flags, int mode)
{
	int fd;

	if ((fd = vips__open(pathname, flags, mode)) == -1)
		return -1;

	vips_tracked_init();

	g_mutex_lock(vips_tracked_mutex);

	vips_tracked_files += 1;
#ifdef DEBUG_VERBOSE_FD
	printf("vips_tracked_open: %s = %d (%d)\n",
		pathname, fd, vips_tracked_files);
#endif /*DEBUG_VERBOSE_FD*/

	g_mutex_unlock(vips_tracked_mutex);

	return fd;
}

/**
 * vips_tracked_close:
 * @fd: file to close()
 *
 * Exactly as close(2), but update the number of files currently open via
 * vips_tracked_get_files(). This is used
 * by the vips operation cache to drop cache when the number of files
 * available is low.
 *
 * You must only close file descriptors opened with vips_tracked_open().
 *
 * See also: vips_tracked_open(), vips_tracked_get_files().
 *
 * Returns: a file descriptor, or -1 on error.
 */
int
vips_tracked_close(int fd)
{
	int result;

	g_mutex_lock(vips_tracked_mutex);

	/* libvips uses fd -1 to mean invalid descriptor.
	 */
	g_assert(fd != -1);
	g_assert(vips_tracked_files > 0);

	vips_tracked_files -= 1;
#ifdef DEBUG_VERBOSE_FD
	printf("vips_tracked_close: %d (%d)\n", fd, vips_tracked_files);
	printf("   from thread %p\n", g_thread_self());
#endif /*DEBUG_VERBOSE_FD*/

	g_mutex_unlock(vips_tracked_mutex);

	result = close(fd);

	return result;
}

/**
 * vips_tracked_get_mem:
 *
 * Returns the number of bytes currently allocated via vips_malloc() and
 * friends. vips uses this figure to decide when to start dropping cache, see
 * #VipsOperation.
 *
 * Returns: the number of currently allocated bytes
 */
size_t
vips_tracked_get_mem(void)
{
	size_t mem;

	vips_tracked_init();

	g_mutex_lock(vips_tracked_mutex);

	mem = vips_tracked_mem;

	g_mutex_unlock(vips_tracked_mutex);

	return mem;
}

/**
 * vips_tracked_get_mem_highwater:
 *
 * Returns the largest number of bytes simultaneously allocated via
 * vips_tracked_malloc(). Handy for estimating max memory requirements for a
 * program.
 *
 * Returns: the largest number of currently allocated bytes
 */
size_t
vips_tracked_get_mem_highwater(void)
{
	size_t mx;

	vips_tracked_init();

	g_mutex_lock(vips_tracked_mutex);

	mx = vips_tracked_mem_highwater;

	g_mutex_unlock(vips_tracked_mutex);

	return mx;
}

/**
 * vips_tracked_get_allocs:
 *
 * Returns the number of active allocations.
 *
 * Returns: the number of active allocations
 */
int
vips_tracked_get_allocs(void)
{
	int n;

	vips_tracked_init();

	g_mutex_lock(vips_tracked_mutex);

	n = vips_tracked_allocs;

	g_mutex_unlock(vips_tracked_mutex);

	return n;
}

/**
 * vips_tracked_get_files:
 *
 * Returns the number of open files.
 *
 * Returns: the number of open files
 */
int
vips_tracked_get_files(void)
{
	int n;

	vips_tracked_init();

	g_mutex_lock(vips_tracked_mutex);

	n = vips_tracked_files;

	g_mutex_unlock(vips_tracked_mutex);

	return n;
}