linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* variable length array on stack
@ 2001-07-24 18:02 Hugh Dickins
  0 siblings, 0 replies; only message in thread
From: Hugh Dickins @ 2001-07-24 18:02 UTC (permalink / raw)
  To: Stelian Pop; +Cc: Alan Cox, linux-kernel

I was surprised to notice a variable length array on stack
in the 2.4.7 (== 2.4.6-ac5) drivers/media/video/meye.c:

static void *ptable_alloc(int npages, u32 *pt_addr) {
	int i = 0;
	void *vmem;
	u32 ptable[npages+1];
	....

I had no idea you could do that!  gcc extension?  Sincere thanks
for teaching me that.  But maybe the construction is inappropriate
in the kernel?  In practice npages here is always 1024, which means
a deeper stack than we'd like to risk.  Wouldn't the (untested)
patch below be better?  Am I missing something when I substitute
kvirt_to_bus(adr) for virt_to_bus(__va(kvirt_to_pa(adr)))?

Hugh

--- linux-2.4.7/drivers/media/video/meye.c	Wed Jul  4 22:41:33 2001
+++ linux/drivers/media/video/meye.c	Tue Jul 24 18:17:08 2001
@@ -209,28 +209,23 @@
 
 /* return a page table pointing to N pages of locked memory */
 static void *ptable_alloc(int npages, u32 *pt_addr) {
-	int i = 0;
+	int i;
 	void *vmem;
-	u32 ptable[npages+1];
-	signed long size;
+	u32 *ptable;
 	unsigned long adr;
 
-	size = (npages + 1) * PAGE_SIZE;
-	vmem = rvmalloc(size);
+	vmem = rvmalloc((npages + 1) * PAGE_SIZE);
 	if (!vmem)
 		return NULL;
 
-	memset(ptable, 0, sizeof(ptable));
-        adr = (unsigned long)vmem;
-	while (size > 0) {
-		ptable[i++] = virt_to_bus(__va(kvirt_to_pa(adr)));
+	adr = (unsigned long)vmem;
+	ptable = (u32 *)(vmem + npages * PAGE_SIZE);
+	for (i = 0; i < npages; i++) {
+		ptable[i] = (u32) kvirt_to_bus(adr);
 		adr += PAGE_SIZE;
-		size -= PAGE_SIZE;
 	}
 
-	memcpy(vmem + npages * PAGE_SIZE, ptable, PAGE_SIZE);
-	*pt_addr = ptable[npages];
-
+	*pt_addr = (u32) kvirt_to_bus(adr);
 	return vmem;
 }
 


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2001-07-24 18:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-24 18:02 variable length array on stack Hugh Dickins

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).