1158486a6SFrançois Tigeot /* 2*f1cbac97SFranç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 37158486a6SFrançois Tigeot #define kfree(ptr) do { \ 38158486a6SFrançois Tigeot if (ptr != NULL) \ 391dedbd3bSFrançois Tigeot kfree((void *)ptr, M_DRM); \ 40158486a6SFrançois Tigeot } while (0) 41158486a6SFrançois Tigeot 42e0b9e154SSascha Wildner #define kcalloc(n, size, flags) kzalloc((n) * (size), flags) 4329f241cfSFrançois Tigeot 4439cfddd2SFrançois Tigeot static inline void * 4539cfddd2SFrançois Tigeot kmalloc_array(size_t n, size_t size, gfp_t flags) 4639cfddd2SFrançois Tigeot { 4739cfddd2SFrançois Tigeot return kmalloc(n * size, M_DRM, flags); 4839cfddd2SFrançois Tigeot } 4939cfddd2SFrançois Tigeot 501dedbd3bSFrançois Tigeot #include <linux/kasan.h> 511dedbd3bSFrançois Tigeot 52*f1cbac97SFrançois Tigeot struct kmem_cache { 53*f1cbac97SFrançois Tigeot unsigned int size; 54*f1cbac97SFrançois Tigeot }; 55*f1cbac97SFrançois Tigeot 56*f1cbac97SFrançois Tigeot #define SLAB_HWCACHE_ALIGN (1 << 0) 57*f1cbac97SFrançois Tigeot #define SLAB_RECLAIM_ACCOUNT (1 << 1) 58*f1cbac97SFrançois Tigeot #define SLAB_DESTROY_BY_RCU (1 << 2) 59*f1cbac97SFrançois Tigeot 60*f1cbac97SFrançois Tigeot #define KMEM_CACHE(__struct, flags) \ 61*f1cbac97SFrançois Tigeot kmem_cache_create(#__struct, sizeof(struct __struct), \ 62*f1cbac97SFrançois Tigeot __alignof(struct __struct), (flags), NULL) 63*f1cbac97SFrançois Tigeot 64*f1cbac97SFrançois Tigeot static inline struct kmem_cache * 65*f1cbac97SFrançois Tigeot kmem_cache_create(const char *name, size_t size, size_t align, 66*f1cbac97SFrançois Tigeot unsigned long flags, void (*ctor)(void *)) 67*f1cbac97SFrançois Tigeot { 68*f1cbac97SFrançois Tigeot struct kmem_cache *kc; 69*f1cbac97SFrançois Tigeot 70*f1cbac97SFrançois Tigeot kc = kzalloc(sizeof(struct kmem_cache), flags); 71*f1cbac97SFrançois Tigeot kc->size = size; 72*f1cbac97SFrançois Tigeot 73*f1cbac97SFrançois Tigeot return kc; 74*f1cbac97SFrançois Tigeot } 75*f1cbac97SFrançois Tigeot 76*f1cbac97SFrançois Tigeot static inline void 77*f1cbac97SFrançois Tigeot kmem_cache_destroy(struct kmem_cache *kc) 78*f1cbac97SFrançois Tigeot { 79*f1cbac97SFrançois Tigeot kfree(kc); 80*f1cbac97SFrançois Tigeot } 81*f1cbac97SFrançois Tigeot 82*f1cbac97SFrançois Tigeot static inline void * 83*f1cbac97SFrançois Tigeot kmem_cache_alloc(struct kmem_cache *kc, gfp_t flags) 84*f1cbac97SFrançois Tigeot { 85*f1cbac97SFrançois Tigeot return kmalloc(kc->size, M_DRM, flags); 86*f1cbac97SFrançois Tigeot } 87*f1cbac97SFrançois Tigeot 88*f1cbac97SFrançois Tigeot static inline void * 89*f1cbac97SFrançois Tigeot kmem_cache_zalloc(struct kmem_cache *kc, gfp_t flags) 90*f1cbac97SFrançois Tigeot { 91*f1cbac97SFrançois Tigeot return kzalloc(kc->size, flags); 92*f1cbac97SFrançois Tigeot } 93*f1cbac97SFrançois Tigeot 94*f1cbac97SFrançois Tigeot static inline void 95*f1cbac97SFrançois Tigeot kmem_cache_free(struct kmem_cache *kc, void *ptr) 96*f1cbac97SFrançois Tigeot { 97*f1cbac97SFrançois Tigeot kfree(ptr); 98*f1cbac97SFrançois Tigeot } 991dedbd3bSFrançois Tigeot 100158486a6SFrançois Tigeot #endif /* _LINUX_SLAB_H_ */ 101