Line data Source code
1 : /* $OpenBSD: drm_memory.c,v 1.27 2015/06/04 06:07:23 jsg Exp $ */
2 : /*-
3 : *Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
4 : * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
5 : * All Rights Reserved.
6 : *
7 : * Permission is hereby granted, free of charge, to any person obtaining a
8 : * copy of this software and associated documentation files (the "Software"),
9 : * to deal in the Software without restriction, including without limitation
10 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 : * and/or sell copies of the Software, and to permit persons to whom the
12 : * Software is furnished to do so, subject to the following conditions:
13 : *
14 : * The above copyright notice and this permission notice (including the next
15 : * paragraph) shall be included in all copies or substantial portions of the
16 : * Software.
17 : *
18 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 : * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 : * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 : * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 : * OTHER DEALINGS IN THE SOFTWARE.
25 : *
26 : * Authors:
27 : * Rickard E. (Rik) Faith <faith@valinux.com>
28 : * Gareth Hughes <gareth@valinux.com>
29 : *
30 : */
31 :
32 : /** @file drm_memory.c
33 : * Wrappers for kernel memory allocation routines, and MTRR management support.
34 : *
35 : * This file previously implemented a memory consumption tracking system using
36 : * the "area" argument for various different types of allocations, but that
37 : * has been stripped out for now.
38 : */
39 :
40 : #include "drmP.h"
41 :
42 : #if !defined(__amd64__) && !defined(__i386__)
43 : #define DRM_NO_MTRR 1
44 : #endif
45 :
46 : void*
47 0 : drm_alloc(size_t size)
48 : {
49 0 : return (malloc(size, M_DRM, M_NOWAIT));
50 : }
51 :
52 : void *
53 0 : drm_calloc(size_t nmemb, size_t size)
54 : {
55 0 : if (nmemb == 0 || SIZE_MAX / nmemb < size)
56 0 : return (NULL);
57 : else
58 0 : return mallocarray(size, nmemb, M_DRM, M_NOWAIT | M_ZERO);
59 0 : }
60 :
61 : void *
62 0 : drm_realloc(void *oldpt, size_t oldsize, size_t size)
63 : {
64 : void *pt;
65 :
66 0 : pt = malloc(size, M_DRM, M_NOWAIT);
67 0 : if (pt == NULL)
68 0 : return NULL;
69 0 : if (oldpt && oldsize) {
70 0 : memcpy(pt, oldpt, min(oldsize, size));
71 0 : free(oldpt, M_DRM, 0);
72 0 : }
73 0 : return pt;
74 0 : }
75 :
76 : void
77 0 : drm_free(void *pt)
78 : {
79 0 : if (pt != NULL)
80 0 : free(pt, M_DRM, 0);
81 0 : }
82 :
83 : int
84 0 : drm_mtrr_add(unsigned long offset, size_t size, int flags)
85 : {
86 : #ifndef DRM_NO_MTRR
87 0 : int act;
88 0 : struct mem_range_desc mrdesc;
89 :
90 0 : mrdesc.mr_base = offset;
91 0 : mrdesc.mr_len = size;
92 0 : mrdesc.mr_flags = flags;
93 0 : act = MEMRANGE_SET_UPDATE;
94 0 : strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
95 0 : return mem_range_attr_set(&mrdesc, &act);
96 : #else
97 : return 0;
98 : #endif
99 0 : }
100 :
101 : int
102 0 : drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags)
103 : {
104 : #ifndef DRM_NO_MTRR
105 0 : int act;
106 0 : struct mem_range_desc mrdesc;
107 :
108 0 : mrdesc.mr_base = offset;
109 0 : mrdesc.mr_len = size;
110 0 : mrdesc.mr_flags = flags;
111 0 : act = MEMRANGE_SET_REMOVE;
112 0 : strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
113 0 : return mem_range_attr_set(&mrdesc, &act);
114 : #else
115 : return 0;
116 : #endif
117 0 : }
|