cregit-Linux how code gets into the kernel

Release 4.11 tools/virtio/ringtest/ptr_ring.c


#define _GNU_SOURCE
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <malloc.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>


#define SMP_CACHE_BYTES 64

#define cache_line_size() SMP_CACHE_BYTES

#define ____cacheline_aligned_in_smp __attribute__ ((aligned (SMP_CACHE_BYTES)))

#define unlikely(x)    (__builtin_expect(!!(x), 0))

#define likely(x)    (__builtin_expect(!!(x), 1))

#define ALIGN(x, a) (((x) + (a) - 1) / (a) * (a))

typedef pthread_spinlock_t  spinlock_t;


typedef int gfp_t;

static void *kmalloc(unsigned size, gfp_t gfp) { return memalign(64, size); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin21100.00%1100.00%
Total21100.00%1100.00%


static void *kzalloc(unsigned size, gfp_t gfp) { void *p = memalign(64, size); if (!p) return p; memset(p, 0, size); return p; }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin44100.00%1100.00%
Total44100.00%1100.00%


static void kfree(void *p) { if (p) free(p); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin19100.00%1100.00%
Total19100.00%1100.00%


static void spin_lock_init(spinlock_t *lock) { int r = pthread_spin_init(lock, 0); assert(!r); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin26100.00%1100.00%
Total26100.00%1100.00%


static void spin_lock(spinlock_t *lock) { int ret = pthread_spin_lock(lock); assert(!ret); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin24100.00%1100.00%
Total24100.00%1100.00%


static void spin_unlock(spinlock_t *lock) { int ret = pthread_spin_unlock(lock); assert(!ret); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin24100.00%1100.00%
Total24100.00%1100.00%


static void spin_lock_bh(spinlock_t *lock) { spin_lock(lock); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin15100.00%1100.00%
Total15100.00%1100.00%


static void spin_unlock_bh(spinlock_t *lock) { spin_unlock(lock); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin15100.00%1100.00%
Total15100.00%1100.00%


static void spin_lock_irq(spinlock_t *lock) { spin_lock(lock); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin15100.00%1100.00%
Total15100.00%1100.00%


static void spin_unlock_irq(spinlock_t *lock) { spin_unlock(lock); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin15100.00%1100.00%
Total15100.00%1100.00%


static void spin_lock_irqsave(spinlock_t *lock, unsigned long f) { spin_lock(lock); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin19100.00%1100.00%
Total19100.00%1100.00%


static void spin_unlock_irqrestore(spinlock_t *lock, unsigned long f) { spin_unlock(lock); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin19100.00%1100.00%
Total19100.00%1100.00%

#include "../../../include/linux/ptr_ring.h" static unsigned long long headcnt, tailcnt; static struct ptr_ring array ____cacheline_aligned_in_smp; /* implemented by ring */
void alloc_ring(void) { int ret = ptr_ring_init(&array, ring_size, 0); assert(!ret); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin26100.00%1100.00%
Total26100.00%1100.00%

/* guest side */
int add_inbuf(unsigned len, void *buf, void *datap) { int ret; ret = __ptr_ring_produce(&array, buf); if (ret >= 0) { ret = 0; headcnt++; } return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin47100.00%1100.00%
Total47100.00%1100.00%

/* * ptr_ring API provides no way for producer to find out whether a given * buffer was consumed. Our tests merely require that a successful get_buf * implies that add_inbuf succeed in the past, and that add_inbuf will succeed, * fake it accordingly. */
void *get_buf(unsigned *lenp, void **bufp) { void *datap; if (tailcnt == headcnt || __ptr_ring_full(&array)) datap = NULL; else { datap = "Buffer\n"; ++tailcnt; } return datap; }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin48100.00%1100.00%
Total48100.00%1100.00%


bool used_empty() { return (tailcnt == headcnt || __ptr_ring_full(&array)); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin1161.11%150.00%
Paolo Bonzini738.89%150.00%
Total18100.00%2100.00%


void disable_call() { assert(0); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin10100.00%1100.00%
Total10100.00%1100.00%


bool enable_call() { assert(0); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin10100.00%1100.00%
Total10100.00%1100.00%


void kick_available(void) { assert(0); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin12100.00%1100.00%
Total12100.00%1100.00%

/* host side */
void disable_kick() { assert(0); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin10100.00%1100.00%
Total10100.00%1100.00%


bool enable_kick() { assert(0); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin10100.00%1100.00%
Total10100.00%1100.00%


bool avail_empty() { return !__ptr_ring_peek(&array); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin753.85%150.00%
Paolo Bonzini646.15%150.00%
Total13100.00%2100.00%


bool use_buf(unsigned *lenp, void **bufp) { void *ptr; ptr = __ptr_ring_consume(&array); return ptr; }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin29100.00%1100.00%
Total29100.00%1100.00%


void call_used(void) { assert(0); }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin12100.00%1100.00%
Total12100.00%1100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin58397.82%375.00%
Paolo Bonzini132.18%125.00%
Total596100.00%4100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.