xref: /dflybsd-src/sys/dev/drm/include/linux/slab.h (revision e9dbfea17a45e13134f19ed8fa22fbe8d11ac99c)
1158486a6SFrançois Tigeot /*
2f1cbac97SFrançois Tigeot  * Copyright (c) 2015-2020 François Tigeot <ftigeot@wolfpond.org>
3158486a6SFrançois Tigeot  * All rights reserved.
4158486a6SFrançois Tigeot  *
5158486a6SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
6158486a6SFrançois Tigeot  * modification, are permitted provided that the following conditions
7158486a6SFrançois Tigeot  * are met:
8158486a6SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
9158486a6SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
10158486a6SFrançois Tigeot  *    disclaimer.
11158486a6SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
12158486a6SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
13158486a6SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
14158486a6SFrançois Tigeot  *
15158486a6SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16158486a6SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17158486a6SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18158486a6SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19158486a6SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20158486a6SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21158486a6SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22158486a6SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23158486a6SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24158486a6SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25158486a6SFrançois Tigeot  */
26158486a6SFrançois Tigeot 
27158486a6SFrançois Tigeot #ifndef _LINUX_SLAB_H_
28158486a6SFrançois Tigeot #define _LINUX_SLAB_H_
29158486a6SFrançois Tigeot 
30f43360b9SFrançois Tigeot #include <linux/gfp.h>
3129f241cfSFrançois Tigeot #include <linux/types.h>
32f43360b9SFrançois Tigeot 
33c8480522SFrançois Tigeot MALLOC_DECLARE(M_DRM);
34c8480522SFrançois Tigeot 
35f43360b9SFrançois Tigeot #define kzalloc(size, flags)	kmalloc(size, M_DRM, flags | M_ZERO)
36f43360b9SFrançois Tigeot 
37*e9dbfea1SMatthew Dillon #undef kfree
38158486a6SFrançois Tigeot #define kfree(ptr)	do {			\
39158486a6SFrançois Tigeot 	if (ptr != NULL)			\
40*e9dbfea1SMatthew Dillon 		_kfree((void *)ptr, M_DRM);	\
41158486a6SFrançois Tigeot } while (0)
42158486a6SFrançois Tigeot 
43e0b9e154SSascha Wildner #define kcalloc(n, size, flags)	kzalloc((n) * (size), flags)
4429f241cfSFrançois Tigeot 
4539cfddd2SFrançois Tigeot static inline void *
kmalloc_array(size_t n,size_t size,gfp_t flags)4639cfddd2SFrançois Tigeot kmalloc_array(size_t n, size_t size, gfp_t flags)
4739cfddd2SFrançois Tigeot {
4839cfddd2SFrançois Tigeot 	return kmalloc(n * size, M_DRM, flags);
4939cfddd2SFrançois Tigeot }
5039cfddd2SFrançois Tigeot 
511dedbd3bSFrançois Tigeot #include <linux/kasan.h>
521dedbd3bSFrançois Tigeot 
53f1cbac97SFrançois Tigeot struct kmem_cache {
54f1cbac97SFrançois Tigeot 	unsigned int size;
55f1cbac97SFrançois Tigeot };
56f1cbac97SFrançois Tigeot 
57a85cb24fSFrançois Tigeot #define SLAB_HWCACHE_ALIGN	0x01UL
58a85cb24fSFrançois Tigeot #define SLAB_RECLAIM_ACCOUNT	0x02UL
59a85cb24fSFrançois Tigeot #define SLAB_TYPESAFE_BY_RCU	0x04UL
60f1cbac97SFrançois Tigeot 
61f1cbac97SFrançois Tigeot #define KMEM_CACHE(__struct, flags)				\
62f1cbac97SFrançois Tigeot 	kmem_cache_create(#__struct, sizeof(struct __struct),	\
63f1cbac97SFrançois Tigeot 	__alignof(struct __struct), (flags), NULL)
64f1cbac97SFrançois Tigeot 
65f1cbac97SFrançois Tigeot static inline struct kmem_cache *
kmem_cache_create(const char * name,size_t size,size_t align,unsigned long flags,void (* ctor)(void *))66f1cbac97SFrançois Tigeot kmem_cache_create(const char *name, size_t size, size_t align,
67f1cbac97SFrançois Tigeot 		  unsigned long flags, void (*ctor)(void *))
68f1cbac97SFrançois Tigeot {
69f1cbac97SFrançois Tigeot 	struct kmem_cache *kc;
70f1cbac97SFrançois Tigeot 
71f1cbac97SFrançois Tigeot 	kc = kzalloc(sizeof(struct kmem_cache), flags);
72f1cbac97SFrançois Tigeot 	kc->size = size;
73f1cbac97SFrançois Tigeot 
74f1cbac97SFrançois Tigeot 	return kc;
75f1cbac97SFrançois Tigeot }
76f1cbac97SFrançois Tigeot 
77f1cbac97SFrançois Tigeot static inline void
kmem_cache_destroy(struct kmem_cache * kc)78f1cbac97SFrançois Tigeot kmem_cache_destroy(struct kmem_cache *kc)
79f1cbac97SFrançois Tigeot {
80f1cbac97SFrançois Tigeot 	kfree(kc);
81f1cbac97SFrançois Tigeot }
82f1cbac97SFrançois Tigeot 
83f1cbac97SFrançois Tigeot static inline void *
kmem_cache_alloc(struct kmem_cache * kc,gfp_t flags)84f1cbac97SFrançois Tigeot kmem_cache_alloc(struct kmem_cache *kc, gfp_t flags)
85f1cbac97SFrançois Tigeot {
86f1cbac97SFrançois Tigeot 	return kmalloc(kc->size, M_DRM, flags);
87f1cbac97SFrançois Tigeot }
88f1cbac97SFrançois Tigeot 
89f1cbac97SFrançois Tigeot static inline void *
kmem_cache_zalloc(struct kmem_cache * kc,gfp_t flags)90f1cbac97SFrançois Tigeot kmem_cache_zalloc(struct kmem_cache *kc, gfp_t flags)
91f1cbac97SFrançois Tigeot {
92f1cbac97SFrançois Tigeot 	return kzalloc(kc->size, flags);
93f1cbac97SFrançois Tigeot }
94f1cbac97SFrançois Tigeot 
95f1cbac97SFrançois Tigeot static inline void
kmem_cache_free(struct kmem_cache * kc,void * ptr)96f1cbac97SFrançois Tigeot kmem_cache_free(struct kmem_cache *kc, void *ptr)
97f1cbac97SFrançois Tigeot {
98f1cbac97SFrançois Tigeot 	kfree(ptr);
99f1cbac97SFrançois Tigeot }
1001dedbd3bSFrançois Tigeot 
101158486a6SFrançois Tigeot #endif	/* _LINUX_SLAB_H_ */
102