WinDBG/Exts/CodeAnalyzer/gdbplus/gdb-7.7/gdb/python/py-heap.c
/* Implementation of gdb.big_blocks()
 * Take one argument of int/long of number of biggest heap blocks
   Returns a list of gdb.Heap_block objects  */
Implementation of gdb.big_blocks()
Take one argument of int/long of number of biggest heap blocks
Returns a list of gdb.Heap_block objects
Implementation of gdb . big_blocks () Take one argument of int / long of number of biggest heap blocks Returns a list of gdb . Heap_block objects
PyObject *gdbpy_big_blocks (PyObject *self, PyObject *args)
{
	PyObject *result = NULL;
	unsigned int n = 0;
	struct heap_block* blocks;
	if (PyTuple_Size (args) == 1)
	{
		PyObject *obj = PyTuple_GetItem (args, 0);
		if (PyInt_Check (obj))
			n = (unsigned int) PyInt_AsLong (obj);
		else
		{
			PyErr_SetString (PyExc_TypeError, _("Expect an integer of big heap blocks."));
			return NULL;
		}
	}
	else
	{
		PyErr_SetString (PyExc_TypeError, _("big_blocks takes one argument."));
		return NULL;
	}
	if (n == 0)
	{
		result = Py_None;
		Py_INCREF (Py_None);
		return result;
	}
	else if (n > 1024)
	{
		PyErr_SetString (PyExc_ValueError, _("Currently the function supports up to 1024."));
		return NULL;
	}
	if (!update_memory_segments_and_heaps())
	{
		PyErr_SetString (PyExc_MemoryError, _("Failed to read and initialize process's heap segments."));
		return NULL;
	}
	blocks = (struct heap_block*) malloc (sizeof(struct heap_block) * n);
	if (!blocks)
	{
		PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory."));
		return NULL;
	}
	memset(blocks, 0, sizeof(struct heap_block) * n);
	if (get_biggest_blocks (blocks, n))
	{
		result = PyList_New(n);
		if (result)
		{
			unsigned int i;
			for (i = 0; i < n && blocks[i].addr; i++)
			{
				heap_block_object* blk_object;
				blk_object = PyObject_New (heap_block_object, &heap_block_object_type);
				if (blk_object)
				{
					blk_object->address = Py_BuildValue("l", blocks[i].addr);
					blk_object->size = Py_BuildValue("l", blocks[i].size);
					blk_object->inuse = Py_BuildValue("b", blocks[i].inuse);
					PyList_SET_ITEM(result, i, (PyObject*)blk_object);
				}
				else
				{
					PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to create heap_block object."));
					return NULL;
				}
			}
			while (i < n)
			{
				PyList_SET_ITEM(result, i, Py_None);
				Py_INCREF (Py_None);
				i++;
			}
		}
	}
	free (blocks);
	return result;
}
PyObject * gdbpy_big_blocks ( PyObject * self , PyObject * args ) { PyObject * result = NULL ; unsigned int n = 0 ; struct heap_block * blocks ; if ( PyTuple_Size ( args ) == 1 ) { PyObject * obj = PyTuple_GetItem ( args , 0 ) ; if ( PyInt_Check ( obj ) ) n = ( unsigned int ) PyInt_AsLong ( obj ) ; else { PyErr_SetString ( PyExc_TypeError , _ ( " " ) ) ; return NULL ; } } else { PyErr_SetString ( PyExc_TypeError , _ ( " " ) ) ; return NULL ; } if ( n == 0 ) { result = Py_None ; Py_INCREF ( Py_None ) ; return result ; } else if ( n > 1024 ) { PyErr_SetString ( PyExc_ValueError , _ ( " " ) ) ; return NULL ; } if ( ! update_memory_segments_and_heaps ( ) ) { PyErr_SetString ( PyExc_MemoryError , _ ( " " ) ) ; return NULL ; } blocks = ( struct heap_block * ) malloc ( sizeof ( struct heap_block ) * n ) ; if ( ! blocks ) { PyErr_SetString ( PyExc_MemoryError , _ ( " " ) ) ; return NULL ; } memset ( blocks , 0 , sizeof ( struct heap_block ) * n ) ; if ( get_biggest_blocks ( blocks , n ) ) { result = PyList_New ( n ) ; if ( result ) { unsigned int i ; for ( i = 0 ; i < n && blocks [ i ] . addr ; i ++ ) { heap_block_object * blk_object ; blk_object = PyObject_New ( heap_block_object , & heap_block_object_type ) ; if ( blk_object ) { blk_object -> address = Py_BuildValue ( " " , blocks [ i ] . addr ) ; blk_object -> size = Py_BuildValue ( " " , blocks [ i ] . size ) ; blk_object -> inuse = Py_BuildValue ( " " , blocks [ i ] . inuse ) ; PyList_SET_ITEM ( result , i , ( PyObject * ) blk_object ) ; } else { PyErr_SetString ( PyExc_MemoryError , _ ( " " ) ) ; return NULL ; } } while ( i < n ) { PyList_SET_ITEM ( result , i , Py_None ) ; Py_INCREF ( Py_None ) ; i ++ ; } } } free ( blocks ) ; return result ; }
Implementation of gdb.big_blocks()
 Take one argument of int/long of number of biggest heap blocks
Returns a list of gdb.Heap_block objects
Implementation of gdb . big_blocks () Take one argument of int / long of number of biggest heap blocks Returns a list of gdb . Heap_block objects
// now ask heap manager // if there are not so many blocks // cleanup