阅读:2651回复:17
0xfe0难道有罪吗??关于用ExAllocatePool分配内存的问题
我用Buffer = ExAllocatePool(PagedPool,WriteLen);分配内存,当writelen<4000多字节的时候,就ok,否则,就兰屏
谁有经验,帮助解决以下 |
|
最新喜欢:lsq77 |
沙发#
发布于:2002-09-29 10:42
是分配蓝屏还是,之后引用地址蓝屏。。。却别很大 :D :D :D
|
|
|
板凳#
发布于:2002-09-29 11:03
哦,准确地说应该是应用地址,感谢楼上的兄弟
|
|
地板#
发布于:2002-09-29 11:12
那就是分配内存失败了,而你又没有检查这个分配的地址。。。看看关于内存分配的DDK说明吧,在低资源情况下会出现的。。。 :D :D :D
|
|
|
地下室#
发布于:2002-09-29 11:24
我在饮用这块内存的时候(IF BUFFER),应该是做了检测的
而且,我的内存肯定不是处于低资源状况! 还有,如果分配的直接只要少于0XFE0哪怕一个字节,就没有任何问题 我想应该是分页的问题,郁闷ING |
|
5楼#
发布于:2002-09-29 12:22
分配和使用是否在同一个地方? :D :D :D :D
|
|
|
6楼#
发布于:2002-09-29 13:43
if (WriteLen>BufferSize)//如果缓冲区不够大,从新分配内存
{ BufferSize=WriteLen; if (Buffer!=NULL) { ExFreePool(Buffer); Buffer=NULL; } Buffer = ExAllocatePool(PagedPool,WriteLen); } // Write to shared memory if( BufferSize>0 && Buffer) { RtlCopyMemory( Buffer, Irp->AssociatedIrp.SystemBuffer, WriteLen); } |
|
7楼#
发布于:2002-09-29 13:59
if (WriteLen>BufferSize)//如果缓冲区不够大,从新分配内存 我觉得问题不在分配内存上面。。。。如果分配失败也不会执行COPY了。。。。建议使用 NULL != Buffer 这样的形式来判断。。。 :D :D :D |
|
|
8楼#
发布于:2002-09-29 14:23
我原来就是(!=null)这么用的,出了问题之后,看ddk中的sample就是if (buf)这么用的,就照着改了过来,问题依然存在
现在我都不知道问题出在哪儿了 如果分配内存没有错 有没有可能 DO_BUFFERED_IO方法不支持超过4k字节呢 |
|
9楼#
发布于:2002-09-29 14:46
哦,准确地说应该是应用地址,感谢楼上的兄弟 IRQL== PASSIVE_LEVEL for PagedPool. |
|
|
10楼#
发布于:2002-09-29 15:36
我用Buffer = ExAllocatePool(NonPagedPool,WriteLen);
也是同样的错误啊!!! 我该怎么解决,拜托多写两句! |
|
11楼#
发布于:2002-09-29 15:51
我用Buffer = ExAllocatePool(NonPagedPool,WriteLen); 不是拜托我多写两句, 是拜托你多给两行代码看看。 |
|
|
12楼#
发布于:2002-09-29 16:07
NTSTATUS
PtDispatch( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PIO_STACK_LOCATION irpStack; NTSTATUS status = STATUS_SUCCESS; //ULONG BytesTxd = 0; LONGLONG FilePointer=0; ULONG WriteLen=0; ULONG NewBufferSize=0; PVOID NewBuffer; ULONG ControlCode; ULONG InputLength; ULONG OutputLength; KIRQL irql; PVOID Buff = NULL; DBGPRINT((\"==>Pt Dispatch\\n\")); irpStack = IoGetCurrentIrpStackLocation(Irp); switch (irpStack->MajorFunction) { case IRP_MJ_CREATE: break; case IRP_MJ_CLOSE: break; case IRP_MJ_WRITE: FilePointer = irpStack->Parameters.Write.ByteOffset.QuadPart; WriteLen = irpStack->Parameters.Write.Length; // Get call parameters //DBGPRINT(\"Write %d bytes from file pointer %d\",(int)WriteLen,(int)FilePointer); if( FilePointer<0) status = STATUS_INVALID_PARAMETER; else { // Get access to the shared buffer //KIRQL irql; KeAcquireSpinLock(&BufferLock,&irql); if (WriteLen>BufferSize)//如果缓冲区不够大,从新分配内存 { BufferSize=WriteLen; if (Buffer!=NULL) { ExFreePool(Buffer); Buffer=NULL; } Buffer = ExAllocatePool(PagedPool,WriteLen); } // Write to shared memory if( BufferSize>0 && Buffer) { RtlCopyMemory( Buffer, Irp->AssociatedIrp.SystemBuffer, WriteLen); } else { KeReleaseSpinLock(&BufferLock,irql); return STATUS_INSUFFICIENT_RESOURCES; } // Release shared buffer KeReleaseSpinLock(&BufferLock,irql); } // Complete IRP return CompleteIrp(Irp,status,WriteLen); break; |
|
13楼#
发布于:2002-09-29 16:28
NTSTATUS 1. IoCreateDevice后是否 DeviceObject->Flags |= DO_BUFFER_IO? 2. KeAcquireSpinLock(&BufferLock,&irql); 后,IRQL提升到DISPATCH_LEVEL,在Release前不能使用Unlock Paged Memory,当然在锁定前也必须要Passive_level. 所以改buffer = ExAllocatePool(NonPagedPool,WriteLen); 3. 出错时RETURN时,请IoCompleteRequest结束请求 [编辑 - 9/29/02 by gjpland] |
|
|
14楼#
发布于:2002-09-29 16:31
逻辑:
看了半天 KeAcquireSpinLock(&BufferLock,&irql); 和 KeReleaseSpinLock(&BufferLock,irql); 之间都是涉及局部变量。为什么要用锁呢? |
|
|
15楼#
发布于:2002-09-29 17:01
1. IoCreateDevice后是否 DeviceObject->Flags |= DO_BUFFER_IO?,当然,否则,,小于0xfe0的数据应该也会非法 2. KeAcquireSpinLock(&BufferLock,&irql); 后,IRQL提升到DISPATCH_LEVEL,在Release前不能使用Unlock Paged Memory,当然在锁定前也必须要Passive_level. 所以改buffer = ExAllocatePool(NonPagedPool,WriteLen); 我最开始就是这样做的,结果还是非法 3. 出错时RETURN时,请IoCompleteRequest结束请求 这句话,我不怎么明白 因为buffer是全局变量,所以枷锁 麻烦兄台再帮我看看 [编辑 - 9/29/02 by gjpland] [/quote] |
|
16楼#
发布于:2002-09-29 19:09
试一下,下面我改的代码。
NTSTATUS PtDispatch( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PIO_STACK_LOCATION irpStack; NTSTATUS status = STATUS_SUCCESS; //ULONG BytesTxd = 0; LONGLONG FilePointer=0; ULONG WriteLen=0; ULONG NewBufferSize=0; PVOID NewBuffer; ULONG ControlCode; ULONG InputLength; ULONG OutputLength; KIRQL irql; PVOID Buff = NULL; DBGPRINT((\"==>Pt Dispatch\\n\")); irpStack = IoGetCurrentIrpStackLocation(Irp); switch (irpStack->MajorFunction) { case IRP_MJ_CREATE: break; case IRP_MJ_CLOSE: break; case IRP_MJ_WRITE: FilePointer = irpStack->Parameters.Write.ByteOffset.QuadPart; WriteLen = irpStack->Parameters.Write.Length; // Get call parameters //DBGPRINT(\"Write %d bytes from file pointer %d\",(int)WriteLen,(int)FilePointer); if( FilePointer<0) status = STATUS_INVALID_PARAMETER; else { char *tmpBuffer = NULL; tmpBuffer = ExAllocatePool(NonPagedPool,WriteLen); if( tmpBuffer == NULL ) { CompleteIrp(Irp,STATUS_INSUFFICIENT_RESOURCES,0); return STATUS_INSUFFICIENT_RESOURCES; } memcpy(tmpBuffer,Irp->AssociatedIrp.SystemBuffer,WriteLen); // Get access to the shared buffer //KIRQL irql; KeAcquireSpinLock(&BufferLock,&irql); BufferSize=WriteLen; if (Buffer!=NULL) { ExFreePool(Buffer); Buffer=NULL; } Buffer = tmpBuffer; KeReleaseSpinLock(&BufferLock,irql); } // Complete IRP return CompleteIrp(Irp,status,WriteLen); break; |
|
|
17楼#
发布于:2002-09-30 09:57
多谢两位兄台援手,问题已经解决,分如数奉上,请笑纳
|
|