@@ -31,8 +31,8 @@ public class GitPack : IDisposable
3131 private readonly Func < FileStream > packStream ;
3232 private readonly Lazy < FileStream > indexStream ;
3333 private readonly GitPackCache cache ;
34- private MemoryMappedFile packFile ;
35- private MemoryMappedViewAccessor accessor ;
34+ private MemoryMappedFile ? packFile = null ;
35+ private MemoryMappedViewAccessor ? accessor = null ;
3636
3737 // Maps GitObjectIds to offets in the git pack.
3838 private readonly Dictionary < GitObjectId , long > offsets = new Dictionary < GitObjectId , long > ( ) ;
@@ -98,8 +98,11 @@ public GitPack(GetObjectFromRepositoryDelegate getObjectFromRepositoryDelegate,
9898 this . indexStream = indexStream ?? throw new ArgumentNullException ( nameof ( indexStream ) ) ;
9999 this . cache = cache ?? new GitPackMemoryCache ( ) ;
100100
101- this . packFile = MemoryMappedFile . CreateFromFile ( this . packStream ( ) , mapName : null , 0 , MemoryMappedFileAccess . Read , HandleInheritability . None , leaveOpen : false ) ;
102- this . accessor = this . packFile . CreateViewAccessor ( 0 , 0 , MemoryMappedFileAccess . Read ) ;
101+ if ( IntPtr . Size > 4 )
102+ {
103+ this . packFile = MemoryMappedFile . CreateFromFile ( this . packStream ( ) , mapName : null , 0 , MemoryMappedFileAccess . Read , HandleInheritability . None , leaveOpen : false ) ;
104+ this . accessor = this . packFile . CreateViewAccessor ( 0 , 0 , MemoryMappedFileAccess . Read ) ;
105+ }
103106 }
104107
105108 /// <summary>
@@ -202,7 +205,17 @@ public Stream GetObject(long offset, string objectType)
202205 }
203206
204207 var packStream = this . GetPackStream ( ) ;
205- Stream objectStream = GitPackReader . GetObject ( this , packStream , offset , objectType , packObjectType ) ;
208+ Stream objectStream ;
209+
210+ try
211+ {
212+ objectStream = GitPackReader . GetObject ( this , packStream , offset , objectType , packObjectType ) ;
213+ }
214+ catch
215+ {
216+ packStream . Dispose ( ) ;
217+ throw ;
218+ }
206219
207220 return this . cache . Add ( offset , objectStream ) ;
208221 }
@@ -240,8 +253,8 @@ public void Dispose()
240253 this . indexReader . Value . Dispose ( ) ;
241254 }
242255
243- this . accessor . Dispose ( ) ;
244- this . packFile . Dispose ( ) ;
256+ this . accessor ? . Dispose ( ) ;
257+ this . packFile ? . Dispose ( ) ;
245258 this . cache . Dispose ( ) ;
246259 }
247260
@@ -265,7 +278,17 @@ public void Dispose()
265278
266279 private Stream GetPackStream ( )
267280 {
268- return new MemoryMappedStream ( this . accessor ) ;
281+ // On 64-bit processes, we can use Memory Mapped Streams (the address space
282+ // will be large enough to map the entire packfile). On 32-bit processes,
283+ // we directly access the underlying stream.
284+ if ( IntPtr . Size > 4 )
285+ {
286+ return new MemoryMappedStream ( this . accessor ) ;
287+ }
288+ else
289+ {
290+ return this . packStream ( ) ;
291+ }
269292 }
270293
271294 private GitPackIndexReader OpenIndex ( )
0 commit comments