source: rtems-libbsd/rtemsbsd/powerpc/include/linux/genalloc.h @ 9789434

55-freebsd-126-freebsd-12
Last change on this file since 9789434 was cd089b9, checked in by Sebastian Huber <sebastian.huber@…>, on 05/05/17 at 06:47:39

Linux update to 4.11-rc5

Linux baseline a71c9a1c779f2499fb2afc0553e543f18aff6edf (4.11-rc5).

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/**************************************************************************
2
3Copyright (c) 2007, 2008 Chelsio Inc.
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9 1. Redistributions of source code must retain the above copyright notice,
10    this list of conditions and the following disclaimer.
11
12 2. Neither the name of the Chelsio Corporation nor the names of its
13    contributors may be used to endorse or promote products derived from
14    this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26POSSIBILITY OF SUCH DAMAGE.
27***************************************************************************/
28
29#ifndef _LINUX_GENALLOC_H
30#define _LINUX_GENALLOC_H
31
32#include <sys/blist.h>
33#include <sys/malloc.h>
34
35struct gen_pool {
36        blist_t         gen_list;
37        daddr_t         gen_base;
38        int             gen_chunk_shift;
39        struct mtx      gen_lock;
40};
41
42static inline struct gen_pool *
43gen_pool_create(int min_alloc_order, int nid)
44{
45        struct gen_pool *gp;
46
47        gp = malloc(sizeof(*gp), M_DEVBUF, M_NOWAIT | M_ZERO);
48        if (gp == NULL)
49                return (NULL);
50       
51        gp->gen_chunk_shift = min_alloc_order;
52        mtx_init(&gp->gen_lock, "genpool", NULL, MTX_DEF);
53        return (gp);
54}
55
56static inline int
57gen_pool_add_virt(struct gen_pool *gp, daddr_t virt, daddr_t phys,
58    size_t size, int nid)
59{
60
61        (void)phys;
62        (void)nid;
63
64        if (gp->gen_base != 0)
65                return (-ENOMEM);
66
67        gp->gen_list = blist_create(size >> gp->gen_chunk_shift, M_NOWAIT);
68        if (gp->gen_list == NULL)
69                return (-ENOMEM);
70
71        gp->gen_base = virt;
72        blist_free(gp->gen_list, 0, size >> gp->gen_chunk_shift);
73        return (0);
74}
75
76static inline int
77gen_pool_add(struct gen_pool *gp, daddr_t addr, size_t size, int nid)
78{
79
80        return (gen_pool_add_virt(gp, addr, -1, size, nid));
81}
82
83static inline daddr_t
84gen_pool_alloc(struct gen_pool *gp, size_t size)
85{
86        int chunks;
87        daddr_t blkno;
88
89        chunks = (size + (1 << gp->gen_chunk_shift) - 1) >> gp->gen_chunk_shift;
90        mtx_lock(&gp->gen_lock);
91        blkno = blist_alloc(gp->gen_list, chunks);
92        mtx_unlock(&gp->gen_lock);
93
94        if (blkno == SWAPBLK_NONE)
95                return (0);
96
97        return (gp->gen_base + ((1 << gp->gen_chunk_shift) * blkno));
98}
99
100static inline void
101gen_pool_free(struct gen_pool *gp, daddr_t address, size_t size)
102{
103        int chunks;
104        daddr_t blkno;
105       
106        chunks = (size + (1<<gp->gen_chunk_shift) - 1) >> gp->gen_chunk_shift;
107        blkno = (address - gp->gen_base) / (1 << gp->gen_chunk_shift);
108        mtx_lock(&gp->gen_lock);
109        blist_free(gp->gen_list, blkno, chunks);
110        mtx_unlock(&gp->gen_lock);
111}
112
113static __inline void
114gen_pool_destroy(struct gen_pool *gp)
115{
116        blist_destroy(gp->gen_list);
117        free(gp, M_DEVBUF);
118}
119
120static inline struct gen_pool *
121devm_gen_pool_create(struct device *dev, int min_alloc_order, int nid,
122    const char *name)
123{
124
125        (void)dev;
126        (void)name;
127        return (gen_pool_create(min_alloc_order, nid));
128}
129
130#endif /* _LINUX_GENALLOC_H */
Note: See TracBrowser for help on using the repository browser.