Skip to content

Instantly share code, notes, and snippets.

@tuxillo
Created November 27, 2014 09:12
Show Gist options
  • Save tuxillo/6ee57dac1047345d41b1 to your computer and use it in GitHub Desktop.
Save tuxillo/6ee57dac1047345d41b1 to your computer and use it in GitHub Desktop.
diff --git a/sys/kern/kern_slaballoc.c b/sys/kern/kern_slaballoc.c
index 77386dd..31dc409 100644
--- a/sys/kern/kern_slaballoc.c
+++ b/sys/kern/kern_slaballoc.c
@@ -586,14 +586,28 @@ powerof2_size(unsigned long size)
*
* MPSAFE
*/
-
#ifdef SLAB_DEBUG
void *
kmalloc_debug(unsigned long size, struct malloc_type *type, int flags,
const char *file, int line)
+{
+ _kmalloc_debug(size, type, 1, flags, file, line);
+}
#else
void *
kmalloc(unsigned long size, struct malloc_type *type, int flags)
+{
+ _kmalloc(size, type, 1, flags);
+}
+#endif
+
+#ifdef SLAB_DEBUG
+static void *
+_kmalloc_debug(unsigned long size, struct malloc_type *type, int wait, int flags,
+ const char *file, int line)
+#else
+static void *
+_kmalloc(unsigned long size, struct malloc_type *type, int wait, int flags)
#endif
{
SLZone *z;
@@ -671,7 +685,7 @@ kmalloc(unsigned long size, struct malloc_type *type, int flags)
* might race another cpu allocating the kva and setting
* ku_pagecnt.
*/
- while (slgd->NFreeZones > ZoneRelsThresh && (flags & M_RNOWAIT) == 0) {
+ while (slgd->NFreeZones > ZoneRelsThresh && wait == 0) {
crit_enter();
if (slgd->NFreeZones > ZoneRelsThresh) { /* crit sect race */
int *kup;
@@ -690,7 +704,7 @@ kmalloc(unsigned long size, struct malloc_type *type, int flags)
/*
* XXX handle oversized frees that were queued from kfree().
*/
- while (LIST_FIRST(&slgd->FreeOvZones) && (flags & M_RNOWAIT) == 0) {
+ while (LIST_FIRST(&slgd->FreeOvZones) && wait == 0) {
crit_enter();
if ((z = LIST_FIRST(&slgd->FreeOvZones)) != NULL) {
vm_size_t tsize;
@@ -931,6 +945,20 @@ fail:
return(NULL);
}
+
+/*
+ * kernel malloc nonblocking
+ */
+void *
+kmalloc_nowait(unsigned long size, struct malloc_type *type, int flags)
+{
+#ifdef SLAB_DEBUG
+ _kmalloc_debug(size, type, 0, flags, file, line);
+#else
+ _kmalloc(size, type, 0, flags);
+#endif
+}
+
/*
* kernel realloc. (SLAB ALLOCATOR) (MP SAFE)
*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment