VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/snippets/alloc-1.c@ 53517

Last change on this file since 53517 was 52776, checked in by vboxsync, 10 years ago

fix OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1/* $Id: alloc-1.c 52776 2014-09-17 14:51:43Z vboxsync $ */
2/** @file
3 * Allocate lots of memory, portable ANSI C code.
4 */
5
6/*
7 * Copyright (C) 2007-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <stdlib.h>
31#include <stdio.h>
32
33
34int main(int argc, char **argv)
35{
36 unsigned uPct;
37 unsigned long cbDone;
38 unsigned long cMBs = 1024;
39 unsigned long cb;
40
41 /*
42 * Some quick and dirty argument parsing.
43 */
44 if (argc == 2)
45 cMBs = strtoul(argv[1], 0, 0);
46 if (!cMBs || argc > 2)
47 {
48 printf("usage: alloc-1 [MBs]\n");
49 return 1;
50 }
51 cb = cMBs * 1024 * 1024;
52 if (cb / (1024 * 1024) != cMBs)
53 cb = ~(unsigned long)0 / (1024 * 1024) * (1024 * 1024);
54 printf("alloc-1: allocating %lu MB (%lu bytes)\n", cb/1024/1024, cb);
55
56 /*
57 * The allocation loop.
58 */
59 printf("alloc-1: 0%%");
60 fflush(stdout);
61 cbDone = 0;
62 uPct = 0;
63 while (cbDone < cb)
64 {
65 unsigned uPctNow;
66 unsigned long cbThis = cb > 10*1024*1024 ? 10*1024*1024 : cb;
67 char *pb = malloc(cbThis);
68 if (!pb)
69 {
70 printf("\nalloc-1: calloc failed, cbDone=%lu MB (%lu bytes)\n",
71 cbDone/1024/1024/1024, cbDone);
72 return 1;
73 }
74 cbDone += cbThis;
75
76 /* touch the memory. */
77 while (cbThis >= 0x1000)
78 {
79 *pb = (char)cbThis;
80 pb += 0x1000;
81 cbThis -= 0x1000;
82 }
83
84 /* progress */
85 uPctNow = 100.0 * cbDone / cb;
86 if (uPctNow != uPct && !(uPctNow & 1))
87 {
88 if (!(uPctNow % 10))
89 printf("%u%%", (unsigned)uPctNow);
90 else
91 printf(".");
92 fflush(stdout);
93 }
94 uPct = uPctNow;
95 }
96
97 printf("\nalloc-1: done\n");
98 return 0;
99}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette